【问题标题】:Accessing spring-boot application.properties elements in Junit ClassRule在 Junit ClassRule 中访问 spring-boot application.properties 元素
【发布时间】:2018-12-10 03:16:39
【问题描述】:

我想在 junit 静态类规则中访问资源/属性文件元素(在下面的代码中是 cardEventTopiccardEventErrorTopic 常量)。有什么办法可以做到这一点?由于元素已经存在于属性文件中,我不想将它们作为静态常量放在测试文件中。 @Value("${}") 也不适用于静态上下文。如果有办法使这项工作有任何帮助。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = PaymentAccountUpdaterApplication.class,
                webEnvironment = SpringBootTest.WebEnvironment.NONE)
@TestPropertySource(locations = "classpath:application-messaging.properties")
public class CardIntegrationTest {

    private final static String cardEventTopic = "TP.PRF.CARDEVENTS";
    private final static String cardEventErrorTopic = "TP.PAYMENTS.CARDEVENTS.ERRORS";

    @ClassRule
    public static KafkaEmbedded kafkaEmbedded =
            new KafkaEmbedded(1, false, cardEventTopic, cardEventErrorTopic);
    // rest of the code
}

【问题讨论】:

    标签: spring-boot spring-boot-test


    【解决方案1】:

    您可以通过类注解定义embeddedKafka,并在此处使用spEL定义主题:

    @EmbeddedKafka(topics = {"${my.topic1}", "${my.topic2}"})
    

    或者您可以在任何配置类中将 embededKafka 定义为一个单独的 bean,具有您需要的所有属性。

    别忘了自动接线。

    @Autowired
    private EmbeddedKafkaBroker embeddedKafka;
    

    【讨论】:

      猜你喜欢
      • 2019-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      • 2019-12-27
      • 1970-01-01
      • 2019-07-25
      • 2017-04-28
      相关资源
      最近更新 更多