rabbitmq-mock
rabbitmq-mock copied to clipboard
rabbitTemplate receive() does not dequeue message
I have this simple test. As you can see, calling rabbitTemplate.receive()
did not remove message from the queue, although the same message was received every time I called this method. This issue does not happen if I hook to a real rabbitmq
@TestConfiguration
public class TestConfig {
@Bean
@Primary
ConnectionFactory connectionFactory() {
return new CachingConnectionFactory(new MockConnectionFactory());
}
@Bean
@Primary
public RabbitAdmin getTestRabbitAdmin(ConnectionFactory connectionFactory) {
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
rabbitAdmin.setAutoStartup(true);
return rabbitAdmin;
}
@Bean
@Primary
public RabbitTemplate getTestRabbitTemplate(ConnectionFactory connectionFactory) {
return new RabbitTemplate(connectionFactory);
}
}
@SpringBootTest
@Import(TestConfig.class)
class SampleTest {
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private RabbitAdmin rabbitAdmin;
@Test
void test() throws Exception {
rabbitTemplate.send("test-exchange", "test-key", new Message("test".getBytes(StandardCharsets.UTF_8)));
Thread.sleep(500);
QueueInformation info0 = rabbitAdmin.getQueueInfo("test-queue"); // QueueInformation [name=test-queue, messageCount=1, consumerCount=0]
Message message1 = rabbitTemplate.receive("test-queue", -1);
Thread.sleep(500);
QueueInformation info1 = rabbitAdmin.getQueueInfo("test-queue"); // QueueInformation [name=test-queue, messageCount=1, consumerCount=1]
Message message2 = rabbitTemplate.receive("test-queue", -1);
Thread.sleep(500);
QueueInformation info2 = rabbitAdmin.getQueueInfo("test-queue"); // QueueInformation [name=test-queue, messageCount=1, consumerCount=1]
}
}
I am facing the same issue which is quite unexpected i would say.
Is there any documentation that i am missing?