SpringBoot: how to run tests twice, with two different config files

家住魔仙堡 提交于 2019-12-13 03:19:04

问题


I am writing a SpringBoot application using a JMS MOM. My application supports two kinds of JMS: EMS and AMQ

My application has many Junit Tests. Of course, whether I use EMS or AMQ the tests are exactly the same and the expected results are also exactly the same. The only difference is the configuration file used.

@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() {
        ...
    }

}

What I would like to be able to do is to run my tests twice, one time with an EMS config and one time with and AMQ config: How should I do?

FYI, I am using Maven to build my application. A solution based on a maven trick would be perfectly acceptable for me

Thank you for help


回答1:


It sounds like a task for a building tool that you using.

For example, for Maven you can write specific test tasks with different active profiles, something like this:

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

and collect necessary properties in files: application-{profile}.properties


This articles can help you:

  • how to bind a property file to a current active profile: spring-boot-profile-based-properties

  • mvn profiles: introduction-to-profiles

  • mvn different environments: building-for-different-environments



来源:https://stackoverflow.com/questions/57134667/springboot-how-to-run-tests-twice-with-two-different-config-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!