【问题标题】:SpringBoot: how to run tests twice, with two different config filesSpring Boot:如何使用两个不同的配置文件运行两次测试
【发布时间】:2019-11-29 18:13:30
【问题描述】:

我正在使用 JMS MOM 编写 SpringBoot 应用程序。我的应用程序支持两种 JMS:EMS 和 AMQ

我的应用程序有许多 Junit 测试。当然,无论我使用EMS还是AMQ,测试都是完全一样的,预期的结果也完全一样。唯一的区别是使用的配置文件。

@RunWith(SpringRunner.class)
@TestPropertySource(locations="classpath:application.yaml")
@ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class)
public class MyTest {

    @SpringBootApplication
    @ComponentScan("com.mytest")
    static class Application {
    }


    @Test
    public void test() {
        ...
    }

}

我希望能够运行我的测试两次,一次使用 EMS 配置,另一次使用 AMQ 配置:我应该怎么做?

仅供参考,我正在使用 Maven 构建我的应用程序。基于 maven 技巧的解决方案对我来说是完全可以接受的

感谢您的帮助

【问题讨论】:

    标签: spring maven spring-boot spring-boot-test


    【解决方案1】:

    这听起来像是您使用的构建工具的任务。

    例如,对于 Maven,您可以使用不同的活动配置文件编写特定的测试任务,如下所示:

    mvn clean test -Dspring.profiles.active=kafka
    mvn clean test -Dspring.profiles.active=rabbitmq
    mvn clean test -Dspring.profiles.active=activemq
    

    并在文件中收集必要的属性:application-{profile}.properties


    这篇文章可以帮助你:

    【讨论】:

    • 感谢您的回答;我意识到使用配置文件是最好的选择。伟大的。现在,我需要找到一个解决方案,以便在我运行 mvn clean test 时自动启动我的测试两次:一次使用 EMS 配置文件,另一次使用 AMQ 配置文件。我为此打开了以下问题:stackoverflow.com/questions/57340378/…
    猜你喜欢
    • 2019-02-20
    • 2023-01-22
    • 1970-01-01
    • 2019-12-11
    • 1970-01-01
    • 2021-09-10
    • 2017-02-11
    • 2019-07-11
    • 2019-02-24
    相关资源
    最近更新 更多