I am using embedded Kafka for unit testing a spring boot application. I am using the code - https://github.com/spring-projects/spring-kafka/blob/29a3bd2021c49b700d4f3835c7ced642322c2faf/spring-kafka/src/test/java/org/springframework/kafka/core/reactive/ReactiveKafkaProducerTemplateIntegrationTests.java#L76 as reference.
Here is my code:
@EmbeddedKafka(topics = TestServiceTests.REACTIVE_INT_KEY_TOPIC, partitions = 2)
@ExtendWith(SpringExtension.class)
@EnableKafka
@DirtiesContext
public class TestServiceTests {
public static final String REACTIVE_INT_KEY_TOPIC = "reactive_int_key_topic";
@Autowired
private static ReactiveKafkaConsumerTemplate<String, String> reactiveKafkaConsumerTemplate;
@Autowired
private ReactiveKafkaProducerTemplate<String, List<String>> reactiveKafkaProducerTemplate;
@Before
public void setUpBeforeClass() {
Map<String, Object> consumerProps = KafkaTestUtils
.consumerProps("reactive_consumer_group", "false",EmbeddedKafkaCondition.getBroker());
reactiveKafkaConsumerTemplate =
new ReactiveKafkaConsumerTemplate<String, String>(setupReceiverOptionsWithDefaultTopic(consumerProps));
}
private static ReceiverOptions<String, String> setupReceiverOptionsWithDefaultTopic(
Map<String, Object> consumerProps) {
ReceiverOptions<String, String> basicReceiverOptions = ReceiverOptions.create(consumerProps);
return basicReceiverOptions
.consumerProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest")
.addAssignListener(p -> assertThat(p.iterator().next().topicPartition().topic())
.isEqualTo(REACTIVE_INT_KEY_TOPIC))
.subscription(Collections.singletonList(REACTIVE_INT_KEY_TOPIC));
}
@Test
public void consumeServiceReturnsFlux() {
TestService testService = new TestService(reactiveKafkaConsumerTemplate, reactiveKafkaProducerTemplate);
Flux<String> actual = testService.consumeService();
}
I get the following exception at the line EmbeddedKafkaCondition.getBroker()
java.lang.NullPointerException
at org.springframework.kafka.test.utils.KafkaTestUtils.consumerProps(KafkaTestUtils.java:82)
at ServiceTests.setUpBeforeClass(ServiceTests.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.RunBefores.invokeMethod(RunBefores.java:33)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
java.lang.NullPointerException
at org.springframework.kafka.test.utils.KafkaTestUtils.consumerProps(KafkaTestUtils.java:82)
at com.oracle.gbucs.collect.CollectdCollectorServiceTests.setUpBeforeClass(CollectdCollectorServiceTests.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.RunBefores.invokeMethod(RunBefores.java:33)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Disconnected from the target VM, address: '127.0.0.1:54251', transport: 'socket'
I think I am missing a setting where my embeddedKafka is set not correct and hence is NULL. But I have not been able to figure out the reason. If someone can help me understand what I am missing will be of great help.
Your problem that you use this @ExtendWith(SpringExtension.class)
.
According the EmbeddedKafkaCondition
you can't use its getBroker()
in the Spring context environment:
if (element.isPresent() && !springTestContext(element.get())) {
EmbeddedKafka embedded = AnnotatedElementUtils.findMergedAnnotation(element.get(), EmbeddedKafka.class);
// When running in a spring test context, the EmbeddedKafkaContextCustomizer will create the broker.
So, your ReactiveKafkaConsumerTemplate
must be configured slightly different way:
@Autowired
private EmbeddedKafkaBroker embeddedKafkaBroker;
@Before
public void setUpBeforeClass() {
Map<String, Object> consumerProps = KafkaTestUtils
.consumerProps("reactive_consumer_group", "false", this.embeddedKafkaBroker);
reactiveKafkaConsumerTemplate =
new ReactiveKafkaConsumerTemplate<String, String>(setupReceiverOptionsWithDefaultTopic(consumerProps));
}
UPDATE
Working sample:
@ExtendWith(SpringExtension.class)
@EmbeddedKafka
@EnableKafka
public class WithSpringTestContextTests {
@Autowired
EmbeddedKafkaBroker embeddedKafkaBroker;
@BeforeEach
public void setUpBefore() {
assertThat(embeddedKafkaBroker).isNotNull();
}
@Test
void test() {
}
}