覆盖 application.properties 以在 spring-boot 应用程序中进行集成测试
Override application.properties for integration tests in spring-boot app
我有一个标准的 spring-boot
应用程序,我想在生产环境中使用 MS SQL 数据库,而对于集成测试,我想使用 h2
数据库。问题是我无法找出如何覆盖默认的 application.properties
文件。尽管我尝试遵循一些教程,但我没有想出正确的解决方案……也许我只是遗漏了一些东西……
主要class:
@SpringBootApplication
@EnableTransactionManagement
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication .class, args);
}
}
和 class 测试:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
@WebIntegrationTest
public class MessageControllerTest {
@Autowired
MessageRepository messageRepository;
...
...
...
@Test
public void testSomething(){
...
...
...
...
}
}
所以问题是,如何在 运行 测试时强制 spring-boot 使用 application-test.properties
文件,而不是 application.properties
,这应该是在 运行 时间使用。
例如,我尝试用 @TestPropertySource(locations="classpath:application-test.properties")
替换 @WebIntegrationTest
注释,但这会导致 java.lang.IllegalStateException: Failed to load ApplicationContext
。
假设您的应用程序中有一个 application-test.properties 文件。
我有两种方式:
1.CLI JVM 参数
mvn spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=test
- 添加应用程序-test.properties 作为活动配置文件。
在 application.properties 中添加 spring.profiles.active=test
,它将加载您的应用程序-test.properties 文件。
- 正如您在回答中指出的那样,用特定的活动配置文件注释 class 测试(我认为这不适合进行大型测试 classes )
@ActiveProfiles("test")
实际上这很容易...经过几个小时的尝试,我意识到我只需要用 @ActiveProfiles("test")
注释来注释我的测试 class。
@ActiveProfiles("test")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
@WebIntegrationTest
public class MessageControllerTest {
@Autowired
MessageRepository messageRepository;
...
...
...
@Test
public void testSomething(){
...
...
...
...
}
}
我有一个标准的 spring-boot
应用程序,我想在生产环境中使用 MS SQL 数据库,而对于集成测试,我想使用 h2
数据库。问题是我无法找出如何覆盖默认的 application.properties
文件。尽管我尝试遵循一些教程,但我没有想出正确的解决方案……也许我只是遗漏了一些东西……
主要class:
@SpringBootApplication
@EnableTransactionManagement
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication .class, args);
}
}
和 class 测试:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
@WebIntegrationTest
public class MessageControllerTest {
@Autowired
MessageRepository messageRepository;
...
...
...
@Test
public void testSomething(){
...
...
...
...
}
}
所以问题是,如何在 运行 测试时强制 spring-boot 使用 application-test.properties
文件,而不是 application.properties
,这应该是在 运行 时间使用。
例如,我尝试用 @TestPropertySource(locations="classpath:application-test.properties")
替换 @WebIntegrationTest
注释,但这会导致 java.lang.IllegalStateException: Failed to load ApplicationContext
。
假设您的应用程序中有一个 application-test.properties 文件。
我有两种方式:
1.CLI JVM 参数
mvn spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=test
- 添加应用程序-test.properties 作为活动配置文件。
在 application.properties 中添加 spring.profiles.active=test
,它将加载您的应用程序-test.properties 文件。
- 正如您在回答中指出的那样,用特定的活动配置文件注释 class 测试(我认为这不适合进行大型测试 classes )
@ActiveProfiles("test")
实际上这很容易...经过几个小时的尝试,我意识到我只需要用 @ActiveProfiles("test")
注释来注释我的测试 class。
@ActiveProfiles("test")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
@WebIntegrationTest
public class MessageControllerTest {
@Autowired
MessageRepository messageRepository;
...
...
...
@Test
public void testSomething(){
...
...
...
...
}
}