问题
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