我有一个通过 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.mockito.exceptions.misusing.MissingMethodInvocationException: when() 需要一个参数,该参数必须是“模拟方法调用”。例如:when(mock.getArticles()).thenReturn(articles);
此外,此错误可能会出现,因为: 1. 您存根以下任一方法:final/private/equals()/hashCode() 方法。这些方法不能被存根/验证。2. 在 when() 中,您不会在 mock 上调用方法,而是在其他对象上调用方法。
at com.praxis.fact.cfd.business.impl.ComprobanteServiceImplTests.testSalvarComprobanteConMedioGeneracion(ComprobanteServiceImplTests.java:242)
为什么会发生这种错误?提前致谢。