为什么我得到MissingMethodInvocationException?

时间:2013-06-28 15:51:27

标签: java spring mockito

我有一个通过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()需要一个必须是'对mock进行方法调用'的参数。 例如:     当(mock.getArticles())thenReturn(文章);

此外,此错误可能会显示,因为: 1.你存在以下任何一个:final / private / equals()/ hashCode()方法。    这些方法无法进行存根/验证。 2.在()内部,你不是在模拟上调用方法,而是在其他对象上调用方法。

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

为什么会发生错误? 提前谢谢。

1 个答案:

答案 0 :(得分:2)

唯一的模拟对象是Comprobante的一个实例。但是,您的when()方法会围绕对ComprobanteService实例的调用。 ComprobanteService对象需要是一个模拟,这是错误消息的含义。

请注意,对于此测试,Comprobante对象不需要是mock,尽管您可能希望将其模拟为其他测试。

此外,您不需要使用MockitoAnnotations.initMocks(),因为您没有使用注释。