我正在使用嵌入式 Kafka 对 Spring Boot 应用程序进行单元测试。我正在使用代码 - https://github.com/spring-projects/spring-kafka/blob/29a3bd2021c49b700d4f3835c7ced642322c2faf/spring-kafka/src/test/java/org/springframework/kafka/core/reactive/ReactiveKafkaProducerTemplateIntegrationTests.java# L76作为参考。
这是我的代码:
@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();
}
我在 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'
我想我错过了一个设置,我的嵌入式Kafka 设置不正确,因此为 NULL。但我一直无法弄清楚原因。如果有人可以帮助我了解我所缺少的内容,那将会有很大帮助。