Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 15 additions & 41 deletions spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ If this is not possible for some reason, note that the `consumeFromEmbeddedTopic
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.
====

NOTE: The `EmbeddedKafkaRule` JUnit 4 rule has been removed in version 4.0.
For JUnit 4, you should use the `EmbeddedKafkaKraftBroker` directly or migrate to JUnit 5 with the `@EmbeddedKafka` annotation.
NOTE: `Spring Kafka` no longer supports JUnit4. Please consider migrating from JUnit4 to JUnit5.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our rule is One sentence per line: https://asciidoctor.org/docs/asciidoc-recommended-practices/

And another nit-pick.
Since this is a technical documentation it has to be in a business language and impersonal.
Therefore words, like you, we, please have to be avoided.

I know we have a lot like this in our doc, but that doesn't mean that we have to pollute more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not refer to JUnit Jupiter as "JUnit 5", especially since we (the JUnit team) will be releasing JUnit Jupiter 6 as part of "JUnit 6" later this year.

In other words, I think it is better to refer to JUnit Jupiter as "JUnit Jupiter" to avoid any confusion and unnecessary updates to documentation in the future.

I would also suggest that "JUnit Jupiter" be used consistently across the documentation.


The `EmbeddedKafkaBroker` class has a utility method that lets you consume for all the topics it created.
The following example shows how to use it:
Expand Down Expand Up @@ -121,15 +120,19 @@ The following example configuration creates topics called `cat` and `hat` with f

[source, java]
----
@SpringJUnitConfig
@EmbeddedKafka(
partitions = 5,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not clear from this view, but can we be sure that we don't use tabs for indents?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I had used tabs for indents.
Now, I fixed it with 8 spaces fir indents.
I attached rendered image! Please refer it!

image

topics = {"cat", "hat"}
)
public class MyTests {

@ClassRule
private static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, false, 5, "cat", "hat");
@Autowired
private EmbeddedKafkaBroker broker;

@Test
public void test() {
embeddedKafkaRule.getEmbeddedKafka()
.addTopics(new NewTopic("thing1", 10, (short) 1), new NewTopic("thing2", 15, (short) 1));
broker.addTopics(new NewTopic("thing1", 10, (short) 1), new NewTopic("thing2", 15, (short) 1));
...
}

Expand Down Expand Up @@ -225,7 +228,7 @@ The following example shows how to use it:

[source, java]
----
@RunWith(SpringRunner.class)
@SpringJUnitConfig
@DirtiesContext
@EmbeddedKafka(partitions = 1,
topics = {
Expand All @@ -237,7 +240,7 @@ public class KafkaStreamsTests {
private EmbeddedKafkaBroker embeddedKafka;

@Test
public void someTest() {
void someTest() {
Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("testGroup", "true", this.embeddedKafka);
consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
ConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(consumerProps);
Expand Down Expand Up @@ -288,7 +291,7 @@ In addition, the broker properties are loaded from the `broker.properties` class
Property placeholders are resolved for the `brokerPropertiesLocation` URL and for any property placeholders found in the resource.
Properties defined by `brokerProperties` override properties found in `brokerPropertiesLocation`.

You can use the `@EmbeddedKafka` annotation with JUnit 4 or JUnit 5.
You can use the `@EmbeddedKafka` annotation with JUnit 5.

[[embedded-kafka-junit5]]
== `@EmbeddedKafka` Annotation with JUnit5
Expand Down Expand Up @@ -333,7 +336,7 @@ The following example shows how to do so:
=====
[source, java]
----
@RunWith(SpringRunner.class)
@SpringJUnitConfig
@SpringBootTest(properties = "spring.autoconfigure.exclude="
+ "org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration")
public class MyApplicationTests {
Expand All @@ -347,37 +350,8 @@ There are several ways to use an embedded broker in a Spring Boot application te

They include:

* xref:testing.adoc#kafka-testing-junit4-class-rule[JUnit4 Class Rule]
* xref:testing.adoc#kafka-testing-embeddedkafka-annotation[`@EmbeddedKafka` Annotation or `EmbeddedKafkaBroker` Bean]

[[kafka-testing-junit4-class-rule]]
=== JUnit4 Class Rule

The following example shows how to use a JUnit4 class rule to create an embedded broker:

[source, java]
----
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyApplicationTests {

@ClassRule
public static EmbeddedKafkaRule broker = new EmbeddedKafkaRule(1, false, "someTopic")
.brokerListProperty("spring.kafka.bootstrap-servers");

@Autowired
private KafkaTemplate<String, String> template;

@Test
public void test() {
...
}

}
----

Notice that, since this is a Spring Boot application, we override the broker list property to set Spring Boot's property.

[[embedded-broker-with-springjunitconfig-annotations]]
== `@EmbeddedKafka` with `@SpringJunitConfig`

Expand All @@ -395,7 +369,7 @@ The following example shows how to use an `@EmbeddedKafka` Annotation to create

[source, java]
----
@RunWith(SpringRunner.class)
@SpringJUnitConfig
@EmbeddedKafka(topics = "someTopic",
bootstrapServersProperty = "spring.kafka.bootstrap-servers") // this is now the default
public class MyApplicationTests {
Expand All @@ -404,7 +378,7 @@ public class MyApplicationTests {
private KafkaTemplate<String, String> template;

@Test
public void test() {
void test() {
...
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* <p>
* The typical usage of this annotation is like:
* <pre class="code">
* &#064;RunWith(SpringRunner.class)
* &#064;SpringJUnitConfig
* &#064;EmbeddedKafka
* public class MyKafkaTests {
*
Expand All @@ -67,6 +67,7 @@
* @author Pawel Lozinski
* @author Adrian Chlebosz
* @author Soby Chacko
* @author Sanghyeok An
*
* @since 1.3
*
Expand Down Expand Up @@ -169,4 +170,3 @@
int adminTimeout() default EmbeddedKafkaBroker.DEFAULT_ADMIN_TIMEOUT;

}

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,8 +37,11 @@
* @author Dave Syer
* @author Artem Bilan
* @author Gary Russell
* @author Sanghyeok An
*
* @deprecated since 4.0 in favor of {@link org.springframework.kafka.test.condition.LogLevels}.
*/
@Deprecated(since = "4.0", forRemoval = true)
public class Log4j2LevelAdjuster implements MethodRule {

private final List<Class<?>> classes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.kafka.test.rule;
package org.springframework.kafka.test;

import java.io.IOException;
import java.net.ServerSocket;
Expand All @@ -32,7 +32,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.test.EmbeddedKafkaKraftBroker;
import org.springframework.kafka.test.utils.KafkaTestUtils;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
Expand Down