or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

tessl/maven-org-testcontainers--kafka

Testcontainers Kafka module for automatically instantiating and managing Apache Kafka containers in tests

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.testcontainers/kafka@1.21.x

To install, run

npx @tessl/cli install tessl/maven-org-testcontainers--kafka@1.21.0

0

# Testcontainers Kafka

1

2

Testcontainers Kafka module provides container implementations for running Apache Kafka during integration testing. It supports multiple Kafka distributions including Apache Kafka, Confluent Platform, and legacy compatibility modes with embedded or external Zookeeper and modern KRaft mode for Zookeeper-free deployments.

3

4

## Package Information

5

6

- **Package Name**: org.testcontainers:kafka

7

- **Package Type**: maven

8

- **Language**: Java

9

- **Installation**:

10

11

Maven:

12

```xml

13

<dependency>

14

<groupId>org.testcontainers</groupId>

15

<artifactId>kafka</artifactId>

16

<version>1.21.3</version>

17

<scope>test</scope>

18

</dependency>

19

```

20

21

Gradle:

22

```groovy

23

testImplementation 'org.testcontainers:kafka:1.21.3'

24

```

25

26

## Core Imports

27

28

```java

29

import org.testcontainers.kafka.KafkaContainer;

30

import org.testcontainers.kafka.ConfluentKafkaContainer;

31

import org.testcontainers.utility.DockerImageName;

32

```

33

34

For legacy support (deprecated):

35

```java

36

import org.testcontainers.containers.KafkaContainer;

37

```

38

39

## Basic Usage

40

41

```java

42

import org.testcontainers.kafka.KafkaContainer;

43

import org.testcontainers.utility.DockerImageName;

44

45

// Apache Kafka using modern container

46

try (KafkaContainer kafka = new KafkaContainer("apache/kafka:3.8.0")) {

47

kafka.start();

48

String bootstrapServers = kafka.getBootstrapServers();

49

50

// Use bootstrapServers to configure your Kafka clients

51

// Example: "localhost:32768"

52

}

53

54

// Confluent Platform using modern container

55

try (ConfluentKafkaContainer kafka = new ConfluentKafkaContainer("confluentinc/cp-kafka:7.4.0")) {

56

kafka.start();

57

String bootstrapServers = kafka.getBootstrapServers();

58

}

59

```

60

61

## Architecture

62

63

The Testcontainers Kafka module provides three main container implementations:

64

65

- **Modern Apache Kafka Container**: `org.testcontainers.kafka.KafkaContainer` for apache/kafka and apache/kafka-native images with KRaft mode by default

66

- **Modern Confluent Container**: `org.testcontainers.kafka.ConfluentKafkaContainer` for confluentinc/cp-kafka images (7.4.0+) with KRaft mode by default

67

- **Legacy Container**: `org.testcontainers.containers.KafkaContainer` (deprecated) supporting embedded Zookeeper, external Zookeeper, and KRaft modes

68

- **Shared Helper**: `org.testcontainers.kafka.KafkaHelper` providing common functionality and constants for modern containers

69

70

## Capabilities

71

72

### Apache Kafka Container

73

74

Modern container implementation for Apache Kafka with KRaft mode and flexible listener configuration.

75

76

```java { .api }

77

public class KafkaContainer extends GenericContainer<KafkaContainer> {

78

public KafkaContainer(String imageName);

79

public KafkaContainer(DockerImageName dockerImageName);

80

public KafkaContainer withListener(String listener);

81

public KafkaContainer withListener(String listener, Supplier<String> advertisedListener);

82

public String getBootstrapServers();

83

}

84

```

85

86

[Apache Kafka Container](./apache-kafka-container.md)

87

88

### Confluent Kafka Container

89

90

Modern container implementation for Confluent Platform with KRaft mode and flexible listener configuration.

91

92

```java { .api }

93

public class ConfluentKafkaContainer extends GenericContainer<ConfluentKafkaContainer> {

94

public ConfluentKafkaContainer(String imageName);

95

public ConfluentKafkaContainer(DockerImageName dockerImageName);

96

public ConfluentKafkaContainer withListener(String listener);

97

public ConfluentKafkaContainer withListener(String listener, Supplier<String> advertisedListener);

98

public String getBootstrapServers();

99

}

100

```

101

102

[Confluent Kafka Container](./confluent-kafka-container.md)

103

104

### Legacy Kafka Container (Deprecated)

105

106

Legacy container implementation with Zookeeper and KRaft mode support. Use modern containers instead.

107

108

```java { .api }

109

@Deprecated

110

public class KafkaContainer extends GenericContainer<KafkaContainer> {

111

public KafkaContainer(DockerImageName dockerImageName);

112

public KafkaContainer withEmbeddedZookeeper();

113

public KafkaContainer withExternalZookeeper(String connectString);

114

public KafkaContainer withKraft();

115

public KafkaContainer withClusterId(String clusterId);

116

public KafkaContainer withListener(Supplier<String> listenerSupplier);

117

public String getBootstrapServers();

118

}

119

```

120

121

[Legacy Kafka Container](./legacy-kafka-container.md)

122

123

### Kafka Helper

124

125

Shared utility class providing common constants and functionality for modern Kafka containers.

126

127

```java { .api }

128

class KafkaHelper {

129

static final int KAFKA_PORT = 9092;

130

static final String STARTER_SCRIPT = "/tmp/testcontainers_start.sh";

131

static Map<String, String> envVars();

132

}

133

```

134

135

[Kafka Helper](./kafka-helper.md)

136

137

## Types

138

139

### Docker Image Names

140

141

```java { .api }

142

// Apache Kafka images (for KafkaContainer)

143

DockerImageName.parse("apache/kafka:3.8.0")

144

DockerImageName.parse("apache/kafka-native:3.8.0")

145

146

// Confluent Platform images (for ConfluentKafkaContainer)

147

DockerImageName.parse("confluentinc/cp-kafka:7.4.0")

148

149

// Legacy support (deprecated KafkaContainer)

150

DockerImageName.parse("confluentinc/cp-kafka:6.2.1")

151

```

152

153

### Listener Configuration

154

155

```java { .api }

156

// Simple listener format for modern containers

157

String listener = "kafka:19092";

158

159

// Supplier for dynamic advertised listeners

160

Supplier<String> advertisedListener = () -> "external-host:9092";

161

```

162

163

### Common Container Methods

164

165

All Kafka containers inherit standard Testcontainers functionality:

166

167

```java { .api }

168

// Container lifecycle

169

public void start();

170

public void stop();

171

public boolean isRunning();

172

173

// Network and port access

174

public String getHost();

175

public Integer getMappedPort(int originalPort);

176

public KafkaContainer withNetwork(Network network);

177

public KafkaContainer withNetworkAliases(String... aliases);

178

179

// Environment and configuration

180

public KafkaContainer withEnv(String key, String value);

181

public KafkaContainer withEnv(Map<String, String> env);

182

```

183

184

### Constants

185

186

```java { .api }

187

// Modern containers (KafkaContainer, ConfluentKafkaContainer)

188

static final int KAFKA_PORT = 9092;

189

190

// Legacy container

191

public static final int KAFKA_PORT = 9093;

192

public static final int ZOOKEEPER_PORT = 2181;

193

public static final String DEFAULT_CLUSTER_ID = "4L6g3nShT-eMCtK--X86sw";

194

```