Skip to content

Commit 4c6fe1f

Browse files
added tc kafka implementation
1 parent e657a92 commit 4c6fe1f

8 files changed

Lines changed: 68 additions & 83 deletions

.labspace/01-introduction.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,4 @@ cd workshop
1313
and build the project with Maven:
1414
```bash
1515
./mvnw verify
16-
```
17-
18-
## \(optionally\) Pull the required images before doing the workshop
19-
20-
This might be helpful if the internet connection is somewhat slow.
21-
```bash
22-
docker pull postgres:16-alpine
23-
docker pull redis:7-alpine
24-
docker pull openjdk:8-jre-alpine
25-
docker pull confluentinc/cp-kafka:7.5.0
2616
```

.labspace/02-main-content.md

Lines changed: 0 additions & 49 deletions
This file was deleted.

.labspace/03-adding-some-tests.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ But before we write our first test, let's create an abstract test class for the
77

88
Add `AbstractIntegrationTest` class to `src/test/java` sourceset.
99
It will be an abstract class with standard Spring Boot's testing framework annotations on it:
10-
```plaintext save-as=workshop/src/test/java/com/example/demo/AbstractIntegrationTest.java
10+
```java save-as=workshop/src/test/java/com/example/demo/AbstractIntegrationTest.java
1111
package com.example.demo;
1212

1313
import org.springframework.boot.test.context.SpringBootTest;

.labspace/03-conclusion.md

Lines changed: 0 additions & 18 deletions
This file was deleted.

.labspace/04-your-first-testcontainers-integration.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ It did some pre-flight checks as well to ensure that you have a valid environmen
8585
Changing the PostgreSQL version is as simple as replacing `16-alpine` with, for example, `17-alpine`.
8686
Try it, but don't forget that it will download the new image from the internet, if it's not already present on your computer.
8787

88-
```plaintext save-as=workshop/src/test/java/com/example/demo/AbstractIntegrationTest.java
88+
```java save-as=workshop/src/test/java/com/example/demo/AbstractIntegrationTest.java
8989
package com.example.demo;
9090

9191
import org.springframework.boot.test.context.SpringBootTest;
@@ -96,4 +96,7 @@ import org.springframework.boot.test.context.SpringBootTest;
9696
public class AbstractIntegrationTest {
9797

9898
}
99+
```
100+
```bash
101+
./mvnw clean test
99102
```

.labspace/06-adding-redis.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Luckily, there are the `.start()`/`.stop()` methods of `GenericContainer` to sta
1010

1111
Just update the `AbstractIntegrationTest` with the following code:
1212

13-
```java no-run-button no-copy-button
13+
```java no-run-button
1414
static final GenericContainer redis = new GenericContainer("redis:7-alpine")
1515
.withExposedPorts(6379);
1616

.labspace/07-test-the-api.md

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
Now let's create a test for our API which will verify the business logic.
44

55
```java save-as=workshop/src/test/java/com/example/demo/RatingsControllerTest.java
6-
package com.example.demo.api;
6+
package com.example.demo;
77

88
import com.example.demo.model.Rating;
9-
import com.example.demo.support.AbstractIntegrationTest;
109
import org.junit.jupiter.api.Test;
1110

1211
import static io.restassured.RestAssured.given;
@@ -80,13 +79,73 @@ Test failed. Why?
8079
There is no Kafka!
8180

8281
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.
8483

8584
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+
```
86143

87144
## Hint 1:
88145

89146
Some containers expose helper methods. Check if there is one on `KafkaContainer` which might help you.
147+
![KafkaContainer helpers](.labspace/images/kafka.png)
148+
90149

91150
## Hint 2:
92151

.labspace/images/kafka.png

132 KB
Loading

0 commit comments

Comments
 (0)