Java junit启动时强制停止弹簧启动';结束了

Java junit启动时强制停止弹簧启动';结束了,java,spring,spring-boot,apache-kafka,confluent-platform,Java,Spring,Spring Boot,Apache Kafka,Confluent Platform,实际上,我有一个Spring引导应用程序,我使用了嵌入的kafka,没有使用Spring kafka,而是使用了confluent库 我配置了自定义拓扑,当我启动junit时,流仍然是侦听的,spring引导服务器不会结束 我尝试使用@DirtiesContext,但问题仍然存在 对于我使用的junit的开始 @RunWith(SpringRunner.class) @SpringBootTest 当消费者处于循环中时,我在控制台中看到以下消息: [制作人 clientId=applicati

实际上,我有一个Spring引导应用程序,我使用了嵌入的kafka,没有使用Spring kafka,而是使用了confluent库

我配置了自定义拓扑,当我启动junit时,流仍然是侦听的,spring引导服务器不会结束

我尝试使用@DirtiesContext,但问题仍然存在

对于我使用的junit的开始

@RunWith(SpringRunner.class)
@SpringBootTest
当消费者处于循环中时,我在控制台中看到以下消息:

[制作人 clientId=application1-3c4587c8-23f0-4c8b-8ef0-75bc1e0f966c-StreamThread-1-producer] 无法建立到节点-1的连接。经纪人可能不是 可用

小费


谢谢

看来发生错误是因为代理未运行。但卡夫卡斯特团队正在运行

为了修复它,我认为您需要在测试时阻止KafkaStreams运行

@SpringBootTest注释提供了一个属性类。您可以指定将注册为bean的类

通过指定classes属性,可以防止注册与KafkaStreams相关的bean

例如,您可以按如下方式进行测试。在这种情况下,不会出现上述问题,因为KafkaStreams相关bean没有注册

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ServiceImpl.class, CommonConfig.class})
public class SomeClassTest {
    @Autowired
    private ServiceImpl articleServiceImpl;

    // do test
}
使用@SpringBootTest时,最好只注册要测试的bean

如果您不熟悉这种方法,请尝试模拟管理KafkaStreams对象的bean

例如,我创建以下bean来开始和结束KafkaStreams对象

@Component
public class ManageableStream implements DisposableBean, InitializingBean {

    private final KafkaStreams kafkaStreams;

    public ManageableStream() {
        StreamsConfig config = buildStreamConfig();
        StreamsBuilder builder = new StreamsBuilder();

        KStream<String, String> inputStream = builder.stream("source.topic");
        inputStream.to("destination.topic");

        Topology topology = builder.build();
        kafkaStreams = new KafkaStreams(topology, config);
    }

    @Override
    public void destroy() throws Exception {
        kafkaStreams.close();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        kafkaStreams.start();
    }

    private StreamsConfig buildStreamConfig() {
        Map<String, Object> properties = new HashMap<>();
        properties.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");

        properties.put(StreamsConfig.APPLICATION_ID_CONFIG, "test-stream-application");
        properties.put(StreamsConfig.PROCESSING_GUARANTEE_CONFIG, AT_LEAST_ONCE);
        properties.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.StringSerde.class);
        properties.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.StringSerde.class);

        return new StreamsConfig(properties);
    }
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {

    @MockBean
    private ManageableStream manageableStream;

    @Test
    public void contextLoads() {
    }

}
那么KafkaStreams将不会启动,因此上述问题将不会发生

如果您想测试KafkaStreams的拓扑,请参阅下面的链接


我希望我的回答是有帮助的

你是如何开始junit的?如何验证流仍然存在?你确定你的Kafka实例没有运行吗?我已经添加了更多详细信息以获取答案。我使用@DirtiesContext(classMode=DirtiesContext.classMode.AFTER\u EACH\u TEST\u方法,hierarchyMode=DirtiesContext.hierarchyMode.CURRENT\u LEVEL)解决了这个问题