|
3 | 3 | Now let's create a test for our API which will verify the business logic. |
4 | 4 |
|
5 | 5 | ```java save-as=workshop/src/test/java/com/example/demo/RatingsControllerTest.java |
6 | | -package com.example.demo.api; |
| 6 | +package com.example.demo; |
7 | 7 |
|
8 | 8 | import com.example.demo.model.Rating; |
9 | | -import com.example.demo.support.AbstractIntegrationTest; |
10 | 9 | import org.junit.jupiter.api.Test; |
11 | 10 |
|
12 | 11 | import static io.restassured.RestAssured.given; |
@@ -80,13 +79,73 @@ Test failed. Why? |
80 | 79 | There is no Kafka! |
81 | 80 |
|
82 | 81 | Running Kafka in Docker is easy with Testcontainers. |
83 | | -There is a Testcontainers module providing integration with Kafka and the `KafkaContainer` abstraction for your code. |
| 82 | +There is a [Testcontainers Kafka module](https://java.testcontainers.org/modules/kafka/) providing integration with Kafka and the `KafkaContainer` abstraction for your code. |
84 | 83 |
|
85 | 84 | Just add it the same way as you added Redis and set the `spring.kafka.bootstrap-servers` system property. |
| 85 | +The full `AbstractIntegrationTest` class implementation will look like: |
| 86 | + |
| 87 | +```java save-as=workshop/src/test/java/com/example/demo/AbstractIntegrationTest.java |
| 88 | +package com.example.demo; |
| 89 | + |
| 90 | +import io.restassured.RestAssured; |
| 91 | +import io.restassured.builder.RequestSpecBuilder; |
| 92 | +import io.restassured.specification.RequestSpecification; |
| 93 | +import org.junit.jupiter.api.BeforeEach; |
| 94 | +import org.springframework.boot.test.context.SpringBootTest; |
| 95 | +import org.springframework.boot.test.web.server.LocalServerPort; |
| 96 | +import org.springframework.http.MediaType; |
| 97 | +import org.springframework.test.context.DynamicPropertyRegistry; |
| 98 | +import org.springframework.test.context.DynamicPropertySource; |
| 99 | +import org.testcontainers.containers.GenericContainer; |
| 100 | +import org.testcontainers.containers.KafkaContainer; |
| 101 | +import org.testcontainers.shaded.com.google.common.net.HttpHeaders; |
| 102 | + |
| 103 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { |
| 104 | + "spring.datasource.url=jdbc:tc:postgresql:16-alpine://testcontainers/workshop" |
| 105 | +}) |
| 106 | +public class AbstractIntegrationTest { |
| 107 | + protected RequestSpecification requestSpecification; |
| 108 | + |
| 109 | + @LocalServerPort |
| 110 | + protected int localServerPort; |
| 111 | + |
| 112 | + @BeforeEach |
| 113 | + void setUpAbstractIntegrationTest() { |
| 114 | + RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); |
| 115 | + requestSpecification = new RequestSpecBuilder() |
| 116 | + .setPort(localServerPort) |
| 117 | + .addHeader( |
| 118 | + HttpHeaders.CONTENT_TYPE, |
| 119 | + MediaType.APPLICATION_JSON_VALUE |
| 120 | + ) |
| 121 | + .build(); |
| 122 | + } |
| 123 | + |
| 124 | + static final GenericContainer redis = new GenericContainer("redis:7-alpine") |
| 125 | + .withExposedPorts(6379); |
| 126 | + static final KafkaContainer kafka = new KafkaContainer("7.7.6"); |
| 127 | + |
| 128 | + @DynamicPropertySource |
| 129 | + public static void configureRedisAndKafka(DynamicPropertyRegistry registry) { |
| 130 | + redis.start(); |
| 131 | + kafka.start(); |
| 132 | + registry.add("spring.data.redis.host", redis::getHost); |
| 133 | + registry.add("spring.data.redis.port", redis::getFirstMappedPort); |
| 134 | + registry.add("spring.kafka.bootstrap-servers", kafka::getBootstrapServers); |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +Run test one more time: |
| 140 | +```bash |
| 141 | +./mvnw clean test |
| 142 | +``` |
86 | 143 |
|
87 | 144 | ## Hint 1: |
88 | 145 |
|
89 | 146 | Some containers expose helper methods. Check if there is one on `KafkaContainer` which might help you. |
| 147 | + |
| 148 | + |
90 | 149 |
|
91 | 150 | ## Hint 2: |
92 | 151 |
|
|
0 commit comments