Tuesday, 20 August 2019

kafka test

https://stackoverflow.com/questions/48753051/simple-embedded-kafka-test-example-with-spring-boot
https://codenotfound.com/spring-kafka-embedded-unit-test-example.html


the second one is the best one ,

but you may found that your normal springboot test will stop running and with error looking for ${spring.embedded.kafka.brokers}

the reason is that you added this in your test application.properteis file to make the build-in kafka running ok which uses autowire. (and the build-in kafka will setup "spring.embedded.kafka.brokers" into your env.)

then your normal springboot test will also use this properties file,  and you didn't set the build-in kafka autowire in your test, then there is no "spring.embedded.kafka.brokers"  in your env. then you test will crash.

you can setup an env all add the build-in kafka in you this test. but not good.

so the best way is to change your kafka test. and remove the properties file from your test resource.

make your test use the default properties file from main/resources. and override things in your test by:

@RunWith(SpringRunner.class)
@SpringBootTest(properties = { "kafka.site.topic=test_json_1", "event-hub=false",
"kafka.bootstrap-server=${spring.embedded.kafka.brokers}" })
@DirtiesContext

public class KafkaConsumerTest {


No comments:

Post a comment