You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc
+19-43
Original file line number
Diff line number
Diff line change
@@ -6,10 +6,9 @@ The `spring-kafka-test` jar contains some useful utilities to assist with testin
6
6
[[ekb]]
7
7
== Embedded Kafka Broker
8
8
9
-
Two implementations are provided:
9
+
Since Kafka 4.0 has fully transitioned to KRaft mode, only the `EmbeddedKafkaKraftBroker` implementation is now available:
10
10
11
-
* `EmbeddedKafkaZKBroker` - legacy implementation which starts an embedded `Zookeeper` instance (which is still the default when using `EmbeddedKafka`).
12
-
* `EmbeddedKafkaKraftBroker` - uses `Kraft` instead of `Zookeeper` in combined controller and broker modes (since 3.1).
11
+
* `EmbeddedKafkaKraftBroker` - uses `Kraft` in combined controller and broker modes.
13
12
14
13
There are several techniques to configure the broker as discussed in the following sections.
15
14
@@ -22,7 +21,7 @@ Refer to its javadoc:org.springframework.kafka.test.utils.KafkaTestUtils[Javadoc
22
21
[[junit]]
23
22
== JUnit
24
23
25
-
`org.springframework.kafka.test.utils.KafkaTestUtils` also provides some static methods to set up producer and consumer properties.
24
+
`org.springframework.kafka.test.utils.KafkaTestUtils` provides some static methods to set up producer and consumer properties.
26
25
The following listing shows those method signatures:
27
26
28
27
[source, java]
@@ -57,32 +56,8 @@ If this is not possible for some reason, note that the `consumeFromEmbeddedTopic
57
56
Since it does not have access to the consumer properties, you must use the overloaded method that takes a `seekToEnd` boolean parameter to seek to the end instead of the beginning.
58
57
====
59
58
60
-
A JUnit 4 `@Rule` wrapper for the `EmbeddedKafkaZKBroker` is provided to create an embedded Kafka and an embedded Zookeeper server.
61
-
(See xref:testing.adoc#embedded-kafka-annotation[@EmbeddedKafka Annotation] for information about using `@EmbeddedKafka` with JUnit 5).
62
-
The following listing shows the signatures of those methods:
63
-
64
-
[source, java]
65
-
----
66
-
/**
67
-
* Create embedded Kafka brokers.
68
-
* @param count the number of brokers.
69
-
* @param controlledShutdown passed into TestUtils.createBrokerConfig.
70
-
* @param topics the topics to create (2 partitions per).
71
-
*/
72
-
public EmbeddedKafkaRule(int count, boolean controlledShutdown, String... topics) { ... }
73
-
74
-
/**
75
-
*
76
-
* Create embedded Kafka brokers.
77
-
* @param count the number of brokers.
78
-
* @param controlledShutdown passed into TestUtils.createBrokerConfig.
79
-
* @param partitions partitions per topic.
80
-
* @param topics the topics to create.
81
-
*/
82
-
public EmbeddedKafkaRule(int count, boolean controlledShutdown, int partitions, String... topics) { ... }
83
-
----
84
-
85
-
NOTE: The `EmbeddedKafkaKraftBroker` is not supported with JUnit4.
59
+
NOTE: The `EmbeddedKafkaRule` JUnit 4 rule has been removed in version 4.0.
60
+
For JUnit 4, you should use the `EmbeddedKafkaKraftBroker` directly or migrate to JUnit 5 with the `@EmbeddedKafka` annotation.
86
61
87
62
The `EmbeddedKafkaBroker` class has a utility method that lets you consume for all the topics it created.
88
63
The following example shows how to use it:
@@ -127,8 +102,8 @@ ConsumerRecord<Integer, String> received = KafkaTestUtils.getSingleRecord(consum
127
102
...
128
103
----
129
104
130
-
When the embedded Kafka and embedded Zookeeper server are started by the `EmbeddedKafkaBroker`, a system property named `spring.embedded.kafka.brokers` is set to the address of the Kafka brokers and a system property named `spring.embedded.zookeeper.connect` is set to the address of Zookeeper.
131
-
Convenient constants (`EmbeddedKafkaBroker.SPRING_EMBEDDED_KAFKA_BROKERS` and `EmbeddedKafkaBroker.SPRING_EMBEDDED_ZOOKEEPER_CONNECT`) are provided for this property.
105
+
When the embedded Kafka broker is started by the `EmbeddedKafkaBroker`, a system property named `spring.embedded.kafka.brokers` is set to the address of the Kafka brokers.
106
+
Convenient constants (`EmbeddedKafkaBroker.SPRING_EMBEDDED_KAFKA_BROKERS`) are provided for this property.
132
107
133
108
Instead of default `spring.embedded.kafka.brokers` system property, the address of the Kafka brokers can be exposed to any arbitrary and convenient property.
134
109
For this purpose a `spring.embedded.kafka.brokers.property` (`EmbeddedKafkaBroker.BROKER_LIST_PROPERTY`) system property can be set before starting an embedded Kafka.
@@ -227,8 +202,7 @@ In addition, these properties can be provided:
227
202
- `spring.kafka.embedded.ports` - ports (comma-separated value) for every Kafka broker to start, `0` if random port is preferred; the number of values must be equal to the `count` mentioned above;
228
203
- `spring.kafka.embedded.topics` - topics (comma-separated value) to create in the started Kafka cluster;
229
204
- `spring.kafka.embedded.partitions` - number of partitions to provision for the created topics;
230
-
- `spring.kafka.embedded.broker.properties.location` - the location of the file for additional Kafka broker configuration properties; the value of this property must follow the Spring resource abstraction pattern;
231
-
- `spring.kafka.embedded.kraft` - default false, when true, use an `EmbeddedKafkaKraftBroker` instead of an `EmbeddedKafkaZKBroker`.
205
+
- `spring.kafka.embedded.broker.properties.location` - the location of the file for additional Kafka broker configuration properties; the value of this property must follow the Spring resource abstraction pattern.
232
206
233
207
Essentially these properties mimic some of the `@EmbeddedKafka` attributes.
234
208
@@ -244,7 +218,7 @@ If you wish to use the embedded broker and are NOT using JUnit, you may wish to
244
218
245
219
[[embedded-kafka-annotation]]
246
220
== `@EmbeddedKafka` Annotation
247
-
We generally recommend that you use the rule as a `@ClassRule` to avoid starting and stopping the broker between tests (and use a different topic for each test).
221
+
We generally recommend that you use a single broker instance to avoid starting and stopping the broker between tests (and use a different topic for each test).
248
222
Starting with version 2.0, if you use Spring's test application context caching, you can also declare a `EmbeddedKafkaBroker` bean, so a single broker can be used across multiple test classes.
249
223
For convenience, we provide a test class-level annotation called `@EmbeddedKafka` to register the `EmbeddedKafkaBroker` bean.
250
224
The following example shows how to use it:
@@ -295,7 +269,7 @@ public class KafkaStreamsTests {
295
269
296
270
Starting with version 2.2.4, you can also use the `@EmbeddedKafka` annotation to specify the Kafka ports property.
297
271
298
-
Starting with version 3.2, set the `kraft` property to `true` to use an `EmbeddedKafkaKraftBroker` instead of an `EmbeddedKafkaZKBroker`.
272
+
NOTE: As of version 4.0, all ZooKeeper-related properties have been removed from the `@EmbeddedKafka` annotation since Kafka 4.0 uses KRaft exclusively.
299
273
300
274
The following example sets the `topics`, `brokerProperties`, and `brokerPropertiesLocation` attributes of `@EmbeddedKafka` support property placeholder resolutions:
301
275
@@ -550,17 +524,20 @@ The following example brings together most of the topics covered in this chapter
0 commit comments