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

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

问题描述

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

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));

但是在执行时我得到

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

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

原因是由于 Mockito.any(MessageCreator.class) ,但是没有办法测试我的发送方法是否在不在 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天全站免登陆