Skip to content

Commit d03768b

Browse files
authored
Convert Docs for Kafka Module #1158 (#2925)
1 parent eecaa9a commit d03768b

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

docs/modules/kafka.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,45 @@ More precisely Testcontainers uses the official Docker images for [Confluent OSS
1111
## Example
1212

1313
The following field in your JUnit test class will prepare a container running Kafka:
14-
```java
15-
@Rule
16-
public KafkaContainer kafka = new KafkaContainer();
17-
```
14+
<!--codeinclude-->
15+
[JUnit Rule](../../modules/kafka/src/test/java/org/testcontainers/containers/KafkaContainerTest.java) inside_block:junitRule
16+
<!--/codeinclude-->
1817
1918
Now your tests or any other process running on your machine can get access to running Kafka broker by using the following bootstrap server location:
20-
```java
21-
kafka.getBootstrapServers()
22-
```
19+
20+
<!--codeinclude-->
21+
[Bootstrap Servers](../../modules/kafka/src/test/java/org/testcontainers/containers/KafkaContainerTest.java) inside_block:getBootstrapServers
22+
<!--/codeinclude-->
23+
2324

2425
## Options
2526

2627
### Selecting Kafka version
2728

2829
You can select a version of Confluent Platform by passing it to the container's constructor:
29-
```java
30-
new KafkaContainer("4.1.2")
31-
```
30+
<!--codeinclude-->
31+
[Version Constructor](../../modules/kafka/src/test/java/org/testcontainers/containers/KafkaContainerTest.java) inside_block:constructorWithVersion
32+
<!--/codeinclude-->
33+
34+
3235
The correspondence between Confluent Platform versions and Kafka versions can be seen [in Confluent documentation](https://docs.confluent.io/current/installation/versions-interoperability.html#cp-and-apache-kafka-compatibility)
3336

3437
### <a name="zookeeper"></a> Using external Zookeeper
3538

3639
If for some reason you want to use an externally running Zookeeper, then just pass its location during construction:
37-
```java
38-
new KafkaContainer().withExternalZookeeper("localhost:2181")
39-
```
40+
<!--codeinclude-->
41+
[External Zookeeper](../../modules/kafka/src/test/java/org/testcontainers/containers/KafkaContainerTest.java) inside_block:withExternalZookeeper
42+
<!--/codeinclude-->
43+
4044

4145
## Multi-container usage
4246

4347
If your test needs to run some other Docker container which needs access to the Kafka, do the following:
4448

4549
* Run your other container on the same network as Kafka container, e.g.:
46-
```java
47-
new GenericContainer("myImage").withNetwork(kafka.getNetwork())
48-
```
50+
<!--codeinclude-->
51+
[Network](../../modules/kafka/src/test/java/org/testcontainers/containers/KafkaContainerTest.java) inside_block:withKafkaNetwork
52+
<!--/codeinclude-->
4953
* Use `kafka.getNetworkAliases().get(0)+":9092"` as bootstrap server location.
5054
Or just give your Kafka container a network alias of your liking.
5155

modules/kafka/src/test/java/org/testcontainers/containers/KafkaContainerTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.apache.kafka.clients.producer.ProducerRecord;
1111
import org.apache.kafka.common.serialization.StringDeserializer;
1212
import org.apache.kafka.common.serialization.StringSerializer;
13+
import org.junit.Rule;
1314
import org.junit.Test;
1415
import org.rnorth.ducttape.unreliables.Unreliables;
1516

@@ -23,6 +24,11 @@
2324

2425
public class KafkaContainerTest {
2526

27+
// junitRule {
28+
@Rule
29+
public KafkaContainer kafka = new KafkaContainer();
30+
// }
31+
2632
@Test
2733
public void testUsage() throws Exception {
2834
try (KafkaContainer kafka = new KafkaContainer()) {
@@ -31,22 +37,48 @@ public void testUsage() throws Exception {
3137
}
3238
}
3339

40+
41+
@Test
42+
public void testUsageWithVersion() throws Exception {
43+
try (
44+
// constructorWithVersion {
45+
KafkaContainer kafka = new KafkaContainer("4.1.2")
46+
// }
47+
) {
48+
kafka.start();
49+
testKafkaFunctionality(
50+
// getBootstrapServers {
51+
kafka.getBootstrapServers()
52+
// }
53+
);
54+
}
55+
}
56+
3457
@Test
3558
public void testExternalZookeeperWithExternalNetwork() throws Exception {
3659
try (
3760
Network network = Network.newNetwork();
3861

62+
// withExternalZookeeper {
3963
KafkaContainer kafka = new KafkaContainer()
4064
.withNetwork(network)
4165
.withExternalZookeeper("zookeeper:2181");
66+
// }
4267

4368
GenericContainer zookeeper = new GenericContainer("confluentinc/cp-zookeeper:4.0.0")
4469
.withNetwork(network)
4570
.withNetworkAliases("zookeeper")
4671
.withEnv("ZOOKEEPER_CLIENT_PORT", "2181");
72+
73+
// withKafkaNetwork {
74+
GenericContainer application = new GenericContainer("alpine").withNetwork(kafka.getNetwork())
75+
// }
76+
.withNetworkAliases("dummy")
77+
.withCommand("sleep 10000")
4778
) {
4879
zookeeper.start();
4980
kafka.start();
81+
application.start();
5082

5183
testKafkaFunctionality(kafka.getBootstrapServers());
5284
}

0 commit comments

Comments
 (0)