springspring-bootspring-kafkajunit5spring-kafka-test

Parallelly running Embedded Kafka spring boot tests


I have some spring boot tests that are using Kafka internally. For Kafka functionality, I am using @EmbeddedKafka annotation on each of the test classes that are using Kafka (with same server on each test, localhost:9092).

@SpringBootTest
@DirtiesContext
@EmbeddedKafka(partitions = 1, brokerProperties = { "listeners=PLAINTEXT://localhost:9092", "port=9092" })
class SampleIntegrationTest extends Base integration test {
   // code
}

The tests are passing when I run individually or sequentially in test suite but failing when I run the test suite parallelly. In junit-platform.properties setting:

junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = same_thread
junit.jupiter.execution.parallel.mode.classes.default = concurrent

Getting resource access exception on some of the tests. Is there someway these tests can be run parallelly with @EmbeddedKafka?

Also just to mention, some of these Kafka related integration tests internally sends data on same topics.

Thanks.


Solution

  • Resolved this by creating an EmbeddedKafkaBroker Bean (creating it conditionally using seperate property file that I use for IntegrationTests). That always starts kafka server at a fixed port and it is can be used by all integration tests, even while running in parallel.

          @Bean
          public EmbeddedKafkaBroker embeddedKafkaBroker() {
            EmbeddedKafkaBroker kafkaEmbedded = new EmbeddedKafkaBroker(COUNT, false, PARTITIONS);
            kafkaEmbedded.brokerProperties(Map.of(
              LISTENERS, LISTENERS_URL,
              PORT, PORT_NUMBER,
              LOG_DIRECTORY, EMBED_KAFKA_LOGS
            ));
            return kafkaEmbedded;
          }