У меня есть служба, которая вызывает DAO через Spring, поэтому теперь я пытаюсь провести некоторое тестирование, используя mock. Вот мой контекст:Почему я получаю 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);
}
Так что, когда я запускаю мой тест I'm получение: org.mockito.exceptions.misusing.MissingMethodInvocationException: when() требует аргумент, который должен быть «вызовом метода на макет». Например: когда (mock.getArticles()). ThenReturn (articles);
Кроме того, эта ошибка может возникнуть из-за того, что: 1. Вы заглушите любой из следующих методов: final/private/equals()/hashCode(). Эти методы не могут быть подвергнуты реставрации/проверке. 2. внутри, когда() вы не вызываете метод на макет, но на какой-то другой объект.
at com.praxis.fact.cfd.business.impl.ComprobanteServiceImplTests.testSalvarComprobanteConMedioGeneracion(ComprobanteServiceImplTests.java:242)
Почему эта ошибка происходит? Спасибо заранее.
Один из ваших комментариев заставил меня понять, что мне не хватает макетной аннотации на Comprobante. Спасибо. – linker85