我有一个通过Spring调用DAO的服务,所以现在我正在尝试使用mock进行一些测试。这是我的上下文:
<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 . mock ITO . exceptions . missingmethodinvocationexception:when()需要一个必须是“模拟上的方法调用”的参数。比如:when(mock.getArticles())。然后返回(文章);
此外,此错误可能因为:1.您存根:final/private/equals()/hashCode() 方法之一。这些方法无法存根/验证。2. 在 when() 内部,您不会在 mock 上调用方法,而是在其他对象上调用方法。
at com.praxis.fact.cfd.business.impl.ComprobanteServiceImplTests.testSalvarComprobanteConMedioGeneracion(ComprobanteServiceImplTests.java:242)
为什么会发生这种错误?提前感谢。
唯一是模拟的对象是 Comprobante 的实例。但是,您的 when() 方法围绕着对 ComprobanteService 实例的调用。ComprobanteService对象需要是一个模拟对象,这就是错误消息的含义。
请注意,对于这个测试,Comprobante对象不需要是模拟对象,尽管您可能希望在其他测试中模拟它。
此外,您不需要使用MockitoAnnotations.initMocks(),因为您没有使用注释。