0

I have a Spring Kafka project, and I have written some unit tests for it. When I execute the test classes, the project attempts to use the actual Kafka broker, and not the embedded Kafka broker as annotated in my test class.

@RunWith(SpringRunner.class)
@SpringBootTest
@EmbeddedKafka
@DirtiesContext
public class SpringKafkaReceiverTest {...}

What I have attempted is checked into gitlab --> https://gitlab.com/mohammad.mnajar/spring-kafka-unit-test

Any help would be extremely appreciated.

Mohammad Najar
  • 2,009
  • 2
  • 21
  • 31

1 Answers1

1

See the documentation.

You need to set the spring.kafka.bootstrap-servers property to the embedded kafka's getBrokersAsString().

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Looks like you have it in `test/application.yml`, but it is commented out (although without `spring` prefix...): https://gitlab.com/mohammad.mnajar/spring-kafka-unit-test/blob/master/src/test/resources/application.yml. Client doesn't care where to connect. It is just uses that `bootstrap-server` property from environment. In case of test you need to override it. That's it. – Artem Bilan Jul 01 '19 at 14:19
  • not sure how it was checked in as commented out. I uncommented the block, and still see the same behavior. The code in repo is updated. – Mohammad Najar Jul 02 '19 at 13:58
  • despite having the bootstrap servers props in my `.yml` file, I explicitly added the prop in the code as `System.setProperty("spring.kafka.bootstrap-servers", embeddedKafkaRule.getEmbeddedKafka().getBrokersAsString())`. Still no luck. – Mohammad Najar Jul 02 '19 at 15:29