Skip to content

Commit

Permalink
Addressing PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
sobychacko committed May 3, 2024
1 parent 689e049 commit 20edc82
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -405,11 +405,12 @@ 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`
== `@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.


Expand Down

0 comments on commit 20edc82

Please sign in to comment.