2013-06-28 1 views
0

스프링을 통해 DAO를 호출하는 서비스가 있으므로 지금 모의를 사용하여 일부 테스트를 수행하려고합니다. 여기 내 상황입니다 :왜 MissingMethodInvocationException이 발생합니까?

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:task="http://www.springframework.org/schema/task" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" 
     default-autowire="byName"> 

    <import resource="classpath*:invoice-core-config-test.xml" /> 
    <import resource="classpath:invoice-cfd-config.xml" /> 
    <import resource="classpath*:invoice-almacenaje-config.xml" /> 
    <import resource="classpath*:invoice-firmadigital-config.xml" /> 
    <bean id="comprobanteServiceMock" class="org.mockito.Mockito" factory-method="mock"> 
     <constructor-arg type="java.lang.Class" 
      value="com.praxis.fact.core.entity.Comprobante" /> 
    </bean> 
</beans> 

그리고 여기 내 서비스 클래스 :

public class ComprobanteServiceImpl implements ComprobanteService { 

    private static final Logger logger = LoggerFactory.getLogger(ComprobanteServiceImpl.class); 

    /** 
    * Dao de comprobantes que se va a utilizar para el servicio. 
    */ 
    @Autowired 
    @Qualifier("comprobanteDao") 
    private ComprobanteDao comprobanteDao; 


    @Override 
    public List<MedioGeneracion> getMediosGeneracion() throws BusinessException { 
     try { 
      if (comprobanteDao == null) {   
       ClassPathXmlApplicationContext ctx = new 
       ClassPathXmlApplicationContext("classpath:invoice-core-config-test.xml"); 
       comprobanteDao = (ComprobanteDao) ctx.getBean("comprobanteDao"); 
      }   
      return comprobanteDao.getMediosGeneracion(); 
     } catch (Exception daoExc) { 
      throw new BusinessException(CodigoError.ERROR_NEGOCIO, "Error al obtener los medios de generacion", daoExc); 
     } 
    } 
} 

그리고 마지막으로 여기 내 시험 방법 : 그래서

@Test 
public void testSalvarComprobanteConMedioGeneracion() { 
    try { 
     ClassPathXmlApplicationContext ctx = new 
       ClassPathXmlApplicationContext("classpath:context.xml"); 
     this.comprobanteTestBean = (Comprobante) ctx.getBean("comprobanteTestBean"); 
     this.comprobanteService = (ComprobanteService)ctx.getBean("comprobanteService"); 
     MockitoAnnotations.initMocks(this); 
     when(comprobanteService.saveComprobante(comprobanteTestBean)).thenReturn(obtenerRespuesta()); 
    } catch (BusinessException e) { 
     logger.error("Error al iniciar el setup() de la prueba", e.getMessage()); 
    } catch (InitializationError e) { 
     logger.error("Ejecuta con: -DfactElectronica.home=C:/tmp"); 
    } 
} 

private Long obtenerRespuesta() { 
    System.out.println("obtenerRespuesta"); 
    return new Long(1); 
} 

내 테스트 미안를 실행 점점 : org.mockito.exceptions.misusing.MissingMethodInvocationException : ()에 '모의 메서드 호출'이되어야하는 인수가 필요한 경우. 예 : (mock.getArticles()) when then return (articles);

또한이 오류는 다음과 같이 표시 될 수 있습니다. 1. final/private/equals()/hashCode() 메소드 중 하나를 스텁합니다. 그 방법 을 스터브/검증 할 수 없습니다. 2. inside()에서 모의 ​​메소드를 호출하지 않고 다른 객체에서 메소드를 호출합니다.

at com.praxis.fact.cfd.business.impl.ComprobanteServiceImplTests.testSalvarComprobanteConMedioGeneracion(ComprobanteServiceImplTests.java:242) 

왜 그런 오류가 발생합니까? 미리 감사드립니다.

답변

2

모의 객체는 Comprobante의 인스턴스입니다. 그러나 when() 메서드는 ComprobanteService 인스턴스에 대한 호출을 둘러 쌉니다. ComprobanteService 객체는 모의 객체 일 필요가 있습니다. 즉, 오류 메시지의 의미입니다.

이 테스트의 경우 Comprobante 객체는 모의 일 필요는 없지만 다른 테스트를 위해 모의하고 싶을 수도 있습니다.

또한 주석을 사용하지 않으므로 MockitoAnnotations.initMocks()를 사용할 필요가 없습니다.

+0

귀하의 의견 중 하나는 제가 Compobante Thanks에서 모의 ​​주석을 놓치고 있음을 깨닫게했습니다. – linker85

관련 문제