我有一个标准的spring-boot
应用程序,我想为生产环境使用MS SQL数据库,而对于集成测试,我想使用h2
数据库。问题是我无法知道如何覆盖默认的application.properties
文件。尽管我试图遵循一些教程,我没有拿出合适的解决方案...也许我只是失去了一些东西......覆盖在spring-boot应用程序中进行集成测试的application.properties
主类:
@SpringBootApplication
@EnableTransactionManagement
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication .class, args);
}
}
和类测试:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
@WebIntegrationTest
public class MessageControllerTest {
@Autowired
MessageRepository messageRepository;
...
...
...
@Test
public void testSomething(){
...
...
...
...
}
}
所以,问题是,如何强制春季启动运行测试的时候,而不是application.properties
,应在运行时使用使用application-test.properties
文件。
我试过例如用@TestPropertySource(locations="classpath:application-test.properties")
替换@WebIntegrationTest
注释,但是这导致java.lang.IllegalStateException: Failed to load ApplicationContext
。
您是否尝试过使用特定配置文件? –
不,因为我对这项技术相当陌生,实际上我不知道什么才是最好的方法来实现这个目标。 – Dworza
上面的评论*是你的问题的答案,根据文档:http:/ /docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties – kryger