如何模擬春季rabbitmq/amqp,以便在嘗試自動創建交換/隊列時在Spring Boot Test期間不會失敗?如何在春季啓動測試中模擬彈簧amqp /兔子
鑑於我有一個簡單RabbitListener
將導致隊列和交換被自動創建這樣的:
@Component
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue(value = "myqueue", autoDelete = "true"),
exchange = @Exchange(value = "myexchange", autoDelete = "true", type = "direct"),
key = "mykey")}
)
@RabbitListenerCondition
public class EventHandler {
@RabbitHandler
public void onEvent(Event event) {
...
}
}
一個簡單的Spring引導檢測過程中,像這樣:
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = { Application.class })
@Autowired
private ApplicationContext applicationContext;
@Test
public void test() {
assertNotNull(applicationContext);
}
}
它會失敗與:
16:22:16.527 [SimpleAsyncTaskExecutor-1] ERROR o.s.a.r.l.SimpleMessageListenerContainer - Failed to check/redeclare auto-delete queue(s).
org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused
at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:62)
at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:309)
在這個測試中,我不在乎兔子/ AMQP,那麼我該如何嘲笑整個兔子/ AMQP呢?
奇怪的是,它用於1.5.6 - 我只需要像這樣配置一個Bean:Mockito.mock(AmqpTemplate.class) - 但現在1.6。1這不再工作:( – domi
如果我是正確的,這也意味着我將無法使用任何SpringBoot測試用例與一個完整的容器,當我有這樣的配置:( – domi
嘲笑模板很容易;這是嘲弄經紀人的迴應(確認,退貨,交付),涉及更多,解釋爲「不再有效」 - 「AmqpTemplate」是一個簡單的「界面」; 1.6中沒有任何東西會改變它的嘲諷能力。對於'@ RabbitListener',你將不得不模擬一個監聽器容器 –