-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
SpringBootRabbitMQApplicationTests.java
67 lines (50 loc) · 1.72 KB
/
SpringBootRabbitMQApplicationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.example.consumer;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.containers.RabbitMQContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.waitAtMost;
@SpringBootTest
@Testcontainers
class SpringBootRabbitMQApplicationTests {
@Container
@ServiceConnection
static RabbitMQContainer rabbitmq = new RabbitMQContainer("rabbitmq:4.0.0-alpine");
@Autowired
private AmqpTemplate amqpTemplate;
@Autowired
private TestListener testListener;
@Test
void consumeMessage() {
this.amqpTemplate.convertAndSend("test", "test-data");
waitAtMost(Duration.ofSeconds(30)).untilAsserted(() -> {
assertThat(this.testListener.messages).hasSize(1);
});
}
@TestConfiguration
static class Config {
@Bean
TestListener testListener() {
return new TestListener();
}
}
static class TestListener {
private final List<String> messages = new ArrayList<>();
@RabbitListener(queuesToDeclare = @Queue("test"))
void listen(String data) {
this.messages.add(data);
}
}
}