<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
- 使用注解 @EmbeddedKafka ,引用依赖包中的kafka server, 注意配置文件的证书等配置
@SpringBootTest(webEnviroment=SpringBootTest.WebEnviroment.RANDOM_PORT)
@AutoConfigureWebTestClient(timeout="36000")
@EmbeddedKafka(partitions = 1, brokerProperties={ "listeners=PLAINTEXT://localhost:9095" },topics = { KafkaStreamsTests.STREAMING_TOPIC1, KafkaStreamsTests.STREAMING_TOPIC2 })
public class MyApplicationTests {
@Autowired
private KafkaTemplate<String, String> template;
@Test
public void test() {
...
}
}