Skip to content

Commit 35542b0

Browse files
authored
Update testing docs for 4.0.0 with the updates in EmbeddedKafka
* Update testing docs for 4.0.0 with the updates in EmbeddedKafka Signed-off-by: Soby Chacko <[email protected]> * Addressing PR review --------- Signed-off-by: Soby Chacko <[email protected]>
1 parent 01e250d commit 35542b0

File tree

2 files changed

+21
-45
lines changed

2 files changed

+21
-45
lines changed

spring-kafka-docs/src/main/antora/modules/ROOT/pages/quick-tour.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ However, the quickest way to get started is to use https://start.spring.io[start
5858

5959
This quick tour works with the following versions:
6060

61-
* Apache Kafka Clients 3.7.x
62-
* Spring Framework 6.1.x
61+
* Apache Kafka Clients 4.0.x
62+
* Spring Framework 7.0.0
6363
* Minimum Java version: 17
6464

6565
[[getting-started]]

spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc

+19-43
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ The `spring-kafka-test` jar contains some useful utilities to assist with testin
66
[[ekb]]
77
== Embedded Kafka Broker
88

9-
Two implementations are provided:
9+
Since Kafka 4.0 has fully transitioned to KRaft mode, only the `EmbeddedKafkaKraftBroker` implementation is now available:
1010

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.
1312

1413
There are several techniques to configure the broker as discussed in the following sections.
1514

@@ -22,7 +21,7 @@ Refer to its javadoc:org.springframework.kafka.test.utils.KafkaTestUtils[Javadoc
2221
[[junit]]
2322
== JUnit
2423

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.
2625
The following listing shows those method signatures:
2726

2827
[source, java]
@@ -57,32 +56,8 @@ If this is not possible for some reason, note that the `consumeFromEmbeddedTopic
5756
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.
5857
====
5958

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.
8661

8762
The `EmbeddedKafkaBroker` class has a utility method that lets you consume for all the topics it created.
8863
The following example shows how to use it:
@@ -127,8 +102,8 @@ ConsumerRecord<Integer, String> received = KafkaTestUtils.getSingleRecord(consum
127102
...
128103
----
129104

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.
132107

133108
Instead of default `spring.embedded.kafka.brokers` system property, the address of the Kafka brokers can be exposed to any arbitrary and convenient property.
134109
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:
227202
- `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;
228203
- `spring.kafka.embedded.topics` - topics (comma-separated value) to create in the started Kafka cluster;
229204
- `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.
232206

233207
Essentially these properties mimic some of the `@EmbeddedKafka` attributes.
234208

@@ -244,7 +218,7 @@ If you wish to use the embedded broker and are NOT using JUnit, you may wish to
244218

245219
[[embedded-kafka-annotation]]
246220
== `@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).
248222
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.
249223
For convenience, we provide a test class-level annotation called `@EmbeddedKafka` to register the `EmbeddedKafkaBroker` bean.
250224
The following example shows how to use it:
@@ -295,7 +269,7 @@ public class KafkaStreamsTests {
295269

296270
Starting with version 2.2.4, you can also use the `@EmbeddedKafka` annotation to specify the Kafka ports property.
297271

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.
299273

300274
The following example sets the `topics`, `brokerProperties`, and `brokerPropertiesLocation` attributes of `@EmbeddedKafka` support property placeholder resolutions:
301275

@@ -550,17 +524,20 @@ The following example brings together most of the topics covered in this chapter
550524

551525
[source, java]
552526
----
527+
@EmbeddedKafka(topics = KafkaTemplateTests.TEMPLATE_TOPIC)
553528
public class KafkaTemplateTests {
554529
555-
private static final String TEMPLATE_TOPIC = "templateTopic";
530+
public static final String TEMPLATE_TOPIC = "templateTopic";
556531
557-
@ClassRule
558-
public static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, true, TEMPLATE_TOPIC);
532+
@BeforeAll
533+
public static void setUp() {
534+
embeddedKafka = EmbeddedKafkaCondition.getBroker();
535+
}
559536
560537
@Test
561538
public void testTemplate() throws Exception {
562539
Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("testT", "false",
563-
embeddedKafka.getEmbeddedKafka());
540+
embeddedKafka);
564541
DefaultKafkaConsumerFactory<Integer, String> cf =
565542
new DefaultKafkaConsumerFactory<>(consumerProps);
566543
ContainerProperties containerProperties = new ContainerProperties(TEMPLATE_TOPIC);
@@ -579,9 +556,9 @@ public class KafkaTemplateTests {
579556
container.setBeanName("templateTests");
580557
container.start();
581558
ContainerTestUtils.waitForAssignment(container,
582-
embeddedKafka.getEmbeddedKafka().getPartitionsPerTopic());
559+
embeddedKafka.getPartitionsPerTopic());
583560
Map<String, Object> producerProps =
584-
KafkaTestUtils.producerProps(embeddedKafka.getEmbeddedKafka());
561+
KafkaTestUtils.producerProps(embeddedKafka);
585562
ProducerFactory<Integer, String> pf =
586563
new DefaultKafkaProducerFactory<>(producerProps);
587564
KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf);
@@ -599,7 +576,6 @@ public class KafkaTemplateTests {
599576
assertThat(received, hasPartition(0));
600577
assertThat(received, hasValue("baz"));
601578
}
602-
603579
}
604580
----
605581

0 commit comments

Comments
 (0)