Skip to content

GH-3873: Deprecate JUnit 4 utilities in the project #3878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
58 changes: 49 additions & 9 deletions spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,19 @@ The following example configuration creates topics called `cat` and `hat` with f

[source, java]
----
@ExtendWith(SpringExtension.class)
@EmbeddedKafka(
partitions = 5,
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 +229,7 @@ The following example shows how to use it:

[source, java]
----
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@DirtiesContext
@EmbeddedKafka(partitions = 1,
topics = {
Expand All @@ -237,7 +241,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 @@ -333,7 +337,7 @@ The following example shows how to do so:
=====
[source, java]
----
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(properties = "spring.autoconfigure.exclude="
+ "org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration")
public class MyApplicationTests {
Expand All @@ -350,6 +354,38 @@ 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-embedded-broker]]
=== Junit4 Embedded Broker

The following example shows how to create an embedded broker in Junit4:
[source, java]
----
@SpringBootTest
public class MyApplicationTests {

@Autowired
private final EmbeddedKafkaBroker broker;

@Autowired
private KafkaTemplate<String, String> template;

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

@Configuration
public static class MyConfiguration {
@Bean
public EmbeddedKafkaBroker embeddedKafkaBroker() {
return new EmbeddedKafkaKraftBroker(1, 1, "someTopic");
}

}

}
----

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

Expand Down Expand Up @@ -378,6 +414,10 @@ public class MyApplicationTests {

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

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.
Please refer to xref:kafka-testing-junit4-embedded-broker[Junit4 Embedded Broker]

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

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

[source, java]
----
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@EmbeddedKafka(topics = "someTopic",
bootstrapServersProperty = "spring.kafka.bootstrap-servers") // this is now the default
public class MyApplicationTests {
Expand All @@ -404,7 +444,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;ExtendWith(SpringExtension.class)
* &#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
Expand Up @@ -37,8 +37,11 @@
* @author Dave Syer
* @author Artem Bilan
* @author Gary Russell
*
* @author Sanghyeok An
* @deprecated since Spring for Apache Kafka 4.0 in favor of the
* {@link org.springframework.kafka.test.condition.LogLevels} and JUnit Jupiter.
*/
@Deprecated(since = "4.0")
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