Spring Boot EmbeddedKafka junit4 -> junit5迁移

问题描述

在我的 spring boot 项目中,我将嵌入式 kafka 与 kafka 代理的自定义属性结合用于我的一些单元测试,但我似乎没有找到如何为我的用例配置 spring boot kafka

Junit4 代码(简化)

@RunWith(springrunner.class)
@ContextConfiguration(initializers = {ContextLoadsTest.Initializer.class})
@SpringBoottest(classes = TestConfig.class,webEnvironment = SpringBoottest.WebEnvironment.NONE)
public class ContextLoadsTest {
    @ClassRule
    public static EmbeddedKafkaRule embeddedKafka =
            new EmbeddedKafkaRule(1,false);

@Autowire
Someclass class;

@Test
    public void contextLoads() {
    }

    public static class Initializer
            implements ApplicationContextinitializer<ConfigurableApplicationContext> {
        public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
            System.out.println("bootstrap servers: " + embeddedKafka.getEmbeddedKafka().getbrokersAsstring());
            TestPropertySourceUtils.addInlinedPropertiesToEnvironment(configurableApplicationContext,"eventbus.kafka.url=" + embeddedKafka.getEmbeddedKafka().getbrokersAsstring());
            TestPropertySourceUtils.addInlinedPropertiesToEnvironment(configurableApplicationContext,"commandbus.kafka.url=" + embeddedKafka.getEmbeddedKafka().getbrokersAsstring());
        }
    }

到目前为止我发现的是 Annotation 中的选项来设置自定义属性,但在我的情况下不会这样做,因为我需要设置 2 个属性,而注释只允许一个,在 JUnit 中是否有其他选择5

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)