我想嘲笑@rabbitlistener。为此,我使用rabbitlistenertestharness,但这似乎不起作用。下面是我的项目结构
主要类别
@Component
public class ABC {
@Autowired
private AmqpTemplate amqpTemplate;
@RabbitListener(id="id", queues="${queue}")
public void receiveMessage(String message) {
publishMsg(message);
}
public void publishMsg(String msg){
amqpTemplate.convertAndSend("exchange", "",msg);
}
}
测试等级
@RunWith(SpringRunner.class)
@SpringBootTest
public class ABCTest {
@Autowired
private RabbitListenerTestHarness harness;
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testSendtoQueue() throws Exception {
ABC listener = this.harness.getSpy("id");
assertNotNull(listener);
LatchCountDownAndCallRealMethodAnswer answer = this.harness.getLatchAnswerFor("id", 1);
doAnswer(answer).when(listener).receiveMessage(any(String.class));
String data = getMockString("src/test/resources/rabbitMQ-data.json");
this.rabbitTemplate.convertAndSend("Queue-name", data);
//assertTrue(answer.await(10));
verify(listener,times(1)).receiveMessage("data");
}
}
在上面运行它会给我如下的错误
Wanted but not invoked:
customPublisherSubscriber.receiveMessageFromResponseQ(
"data"
);
-> at aero.sita.sp.spm.service.ABC.testSendtoQueue(CustomPublisherSubscriberTest.java:67)
Actually, there were zero interactions with this mock.
at aero.sita.sp.spm.service.ABC.testSendtoQueue(CustomPublisherSubscriberTest.java:67)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
我指的是https://github.com/spring-projects/spring-amqp/blob/master/src/reference/asciidoc/testing.adoc.
我很难理解,因为我试图验证间谍对象的消息是否收到,但它报告的问题有关的模拟对象。你能在这方面帮助我吗。
暂无答案!
目前还没有任何答案,快来回答吧!