嵌入式Kafka的Null点异常



我正在使用嵌入式Kafka对spring启动应用程序进行单元测试。我使用的代码- 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();
}

我在embeddedkafkcondition . 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'

我想我错过了一个设置,我的embeddedKafka设置不正确,因此是NULL。但我一直没能找出原因。如果有人能帮我理解我错过了什么,那将是很大的帮助。

你使用这个@ExtendWith(SpringExtension.class)的问题。

根据EmbeddedKafkaCondition,你不能在Spring上下文环境中使用它的getBroker():

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.

因此,您的ReactiveKafkaConsumerTemplate必须以稍微不同的方式配置:

@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));
}

工作示例:

@ExtendWith(SpringExtension.class)
@EmbeddedKafka
@EnableKafka
public class WithSpringTestContextTests {
@Autowired
EmbeddedKafkaBroker embeddedKafkaBroker;
@BeforeEach
public void setUpBefore() {
assertThat(embeddedKafkaBroker).isNotNull();
}
@Test
void test() {
}
}

相关内容

  • 没有找到相关文章

最新更新