Mockito-无效使用参数匹配器 [英] Mockito - Invalid use of argument matchers

查看:269
本文介绍了Mockito-无效使用参数匹配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Junit测试,正在测试jms消息发送.我正在使用Spring jmsTemplate来做到这一点.在这里,就像下面的代码一样,我想检查JMS模板是否调用了send消息,而不管它传递的实际参数的值是什么.

I have Junit test that is testing jms message sending. I am using Spring jmsTemplate to to do this. Here I as in the following code I want to check whether the JMS template has called send message regardless what is it in the values of actuall parameters that are passed.

我的发布者方法使用jmsTemplate来发送方法,如下所示.

my publisher method the uses the jmsTemplate to send method looks like following inside..

jmsTemplate.send(jmsQueueProperties.getProperty(key), new MessageCreator()
{
    public Message createMessage(Session session) throws JMSException
    {
        ObjectMessage obj = session.createObjectMessage(dialogueServiceResponse);
        return obj;
}
});

在我的测试中.

JmsTemplate mockTemplate = Mockito.mock(JmsTemplate.class);
...
publisher.publishServiceMessage(response);
....
Mockito.verify(mockTemplate, 
    Mockito.times(1)).send("appointment.queue", 
        Mockito.any(MessageCreator.class));

但是在执行过程中我得到了

But when in the execution i get

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:参数匹配器的无效使用! ....

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! ....

原因是由于 Mockito.any(MessageCreator.class)造成的,但是没有一种方法可以测试我的send方法是否在MessageCreator中没有创建实际对象的情况下得到执行.

Cause is due to Mockito.any(MessageCreator.class) , but isn't there a way to test my send method is getting executed without creating an actual object in the MessageCreator.

更新 还有一种方法可以检查我的 session.createObjectMessage(dialogueServiceResponse)是否被调用

Update And is there a way to check my session.createObjectMessage(dialogueServiceResponse) is getting called as well

推荐答案

我认为消息的其余部分告诉您问题出在哪里.当您对其中一个参数使用参数匹配器时,所有其他参数也必须使用参数匹配器:

I think the rest of the message tells you what the problem is. When you use an argument matcher for one of the arguments, all the other arguments must also use an argument matcher:

Mockito.verify(mockTemplate, Mockito.times(1)).send(
    Mockito.eq("appointment.queue"), 
    Mockito.any(MessageCreator.class));

这篇关于Mockito-无效使用参数匹配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆