GH-3225: Testing docs improvements
Fixes: #3225 * Clarify the usage of `@DirtiesContext` with `@EmbeddedKafka` when using `@SpringJunitConfig`. * Addressing PR review
This commit is contained in:
@@ -243,7 +243,7 @@ NOTE: `spring-kafka-test` has transitive dependencies on `junit-jupiter-api` and
|
||||
If you wish to use the embedded broker and are NOT using JUnit, you may wish to exclude these dependencies.
|
||||
|
||||
[[embedded-kafka-annotation]]
|
||||
== @EmbeddedKafka Annotation
|
||||
== `@EmbeddedKafka` Annotation
|
||||
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).
|
||||
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.
|
||||
For convenience, we provide a test class-level annotation called `@EmbeddedKafka` to register the `EmbeddedKafkaBroker` bean.
|
||||
@@ -317,7 +317,7 @@ Properties defined by `brokerProperties` override properties found in `brokerPro
|
||||
You can use the `@EmbeddedKafka` annotation with JUnit 4 or JUnit 5.
|
||||
|
||||
[[embedded-kafka-junit5]]
|
||||
== @EmbeddedKafka Annotation with JUnit5
|
||||
== `@EmbeddedKafka` Annotation with JUnit5
|
||||
|
||||
Starting with version 2.3, there are two ways to use the `@EmbeddedKafka` annotation with JUnit5.
|
||||
When used with the `@SpringJunitConfig` annotation, the embedded broker is added to the test application context.
|
||||
@@ -404,6 +404,16 @@ public class MyApplicationTests {
|
||||
|
||||
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`
|
||||
|
||||
When using `@EmbeddedKafka` with `@SpringJUnitConfig`, it is recommended to use `@DirtiesContext` on the test class.
|
||||
This is to prevent potential race conditions occurring during the JVM shutdown after running multiple tests in a test suite.
|
||||
For example, without using `@DirtiesContext`, the `EmbeddedKafkaBroker` may shutdown earlier while the application context still needs resources from it.
|
||||
Since every `EmbeddedKafka` test-runs create its own temporary directory, when this race condition occurs, it will produce error log messages indicating that the files that it is trying to delete or cleanup are not available anymore.
|
||||
Adding `@DirtiesContext` will ensure that the application context is cleaned up after each test and not cached, making it less vulnerable to potential resource race conditions like these.
|
||||
|
||||
|
||||
[[kafka-testing-embeddedkafka-annotation]]
|
||||
=== `@EmbeddedKafka` Annotation or `EmbeddedKafkaBroker` Bean
|
||||
|
||||
|
||||
@@ -56,6 +56,10 @@ import org.springframework.test.context.aot.DisabledInAotMode;
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* When using EmbeddedKafka with {@link org.springframework.test.context.junit.jupiter.SpringJUnitConfig},
|
||||
* it is recommended to use {@link org.springframework.test.annotation.DirtiesContext} on the test class,
|
||||
* in order to prevent certain race conditions on JVM shutdown when running multiple tests.
|
||||
*
|
||||
* @author Artem Bilan
|
||||
* @author Elliot Metsger
|
||||
* @author Zach Olauson
|
||||
|
||||
Reference in New Issue
Block a user