7

I am writing a test class that has multiple methods that require Kafka. Each one requires different broker properties, so I want a separate instance of EmbeddedKafka for each. Doing

@EmbeddedKafka(
    partitions = 20,
    topics = {"topic"},
    controlledShutdown = false,
    brokerProperties = {
      "listeners=PLAINTEXT://localhost:9091",
      "port=9091",
      "auto.create.topics.enable=false",
      "delete.topic.enable=true"
    })

Uses the broker properties for each method in the class. I don't want this. One way to overcome this would be to put each test method in a separate class with different broker properties. But I don't want to do this, as this would blow up the number of files I need.

Is there some way around this?

Prashant Pandey
  • 4,332
  • 3
  • 26
  • 44

1 Answers1

7

It's better to use different topics in each test. However, you can add @DirtiesContext to each test method or add @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) to the test class.

fresskoma
  • 25,481
  • 10
  • 85
  • 128
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • 3
    This solution slows down tests considerably. Any alternative without recreating the context? – alex Jun 24 '21 at 08:32
  • I tried to delete the topics before each test but after a while I end up getting: Expected 3 but got 0 partitions java.lang.IllegalStateException: Expected 3 but got 0 partitions at org.springframework.kafka.test.utils.ContainerTestUtils.waitForSingleContainerAssignment(ContainerTestUtils.java:115) any ideas why? – la00 Jul 26 '22 at 07:46