You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

集成测试中KafkaTemplate自动装配失败,报空指针异常

Kafka集成测试中KafkaTemplate空指针问题排查

我拿到一段别人编写的代码,功能是处理输入并向Kafka发送消息。编写集成测试时遇到自动装配问题,报错如下:

java.lang.NullPointerException: Cannot invoke "org.springframework.kafka.core.KafkaTemplate.send(String, Object)" because "this.kafkaTemplate" is null

试过Stack Overflow上的各种建议,比如添加@RunWith(SpringRunner.class)、@SpringBootTest(classes=...)等注解,但问题依旧,怀疑遗漏了依赖注入的基础要点。


相关业务代码

ProcessService类

@Service
class ProcessService {
  private final MsgProducer msgProducer;

  public ProcessService (MsgProducer msgProducer) {
    this.msgProducer = msgProducer;
  }

  public void sendMsg(String message) {
    // 数据处理逻辑
    msgProducer.sendMessage(message);
  }
}

MsgProducer类与ProducerService接口

@Service
@RequiredArgsConstructor
@Component
public class MsgProducer implements ProducerService {
  @Value("${app.kafka.topic}")
  private String topic;

  @Autowired
  private KafkaTemplate<String, String> kafkaTemplate;

  @Override
  public void sendMessage(String message) {
    kafkaTemplate.send(topic, message);
  }
}

public interface ProducerService {
  void sendMessage(String message);
}

Kafka配置类

@EnableKafka
@org.springframework.context.annotation.Configuration
public class Configuration {
  @Value(value="${spring.kafka.bootstrap-servers}")
  private String bootstrap;

  @Bean
  public ProducerFactory<String, String> producerFactory() {
    Map<String, Object> configProps = new HashMap<>();
    configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrap);
    configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    return new DefaultKafkaProducerFactory<>(configProps);
  }

  @Bean
  public KafkaTemplate<String, String> kafkaTemplate() {
    return new Kafkatemplate<>(this.producerFactory()); // 注意:类名拼写错误,应为KafkaTemplate
  }
}

测试代码

class ProcessServiceIT {
  private MsgProducer producer = new MsgProducer();

  @Test
  void testProducer() {
    producer.send() // 注意:方法名错误,应为sendMessage
  }
}

问题根源与修复方案

核心问题

  1. 测试类未启动Spring容器:手动new MsgProducer()创建对象时,Spring并未参与实例化过程,不会自动注入KafkaTemplate和@Value配置的topic参数。
  2. 配置类拼写错误:Kafkatemplate是拼写错误,正确类名为KafkaTemplate,会导致Spring无法创建有效的KafkaTemplate Bean。
  3. 测试方法调用错误:测试代码中调用的send()方法不存在,应为sendMessage。

修复后的测试类

@SpringBootTest
@RunWith(SpringRunner.class) // Spring Boot 2.2+可省略,@SpringBootTest默认使用SpringExtension
class ProcessServiceIT {
  @Autowired
  private MsgProducer producer; // 由Spring注入Bean,而非手动实例化

  @Test
  void testProducer() {
    producer.sendMessage("test message");
  }
}

额外优化建议

  • MsgProducer同时标注了@Service和@Component,保留一个即可,推荐使用@Service。
  • @RequiredArgsConstructor在当前类中未生效,因为kafkaTemplate是通过@Autowired字段注入而非构造器注入。如果要遵循Spring最佳实践,可改用构造器注入:
@Service
public class MsgProducer implements ProducerService {
  private final String topic;
  private final KafkaTemplate<String, String> kafkaTemplate;

  public MsgProducer(@Value("${app.kafka.topic}") String topic, KafkaTemplate<String, String> kafkaTemplate) {
    this.topic = topic;
    this.kafkaTemplate = kafkaTemplate;
  }

  @Override
  public void sendMessage(String message) {
    kafkaTemplate.send(topic, message);
  }
}

内容的提问来源于stack exchange,提问作者Frank

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 09:05:28