由于旧的Oracle DB,我需要在我的项目中使用 Flyway 4.2.0 . 使用Spring Boot 2.1.0,Flyway包含在Spring Core中,因此我需要以编程方式创建Flyway Bean .
@Bean(initMethod = "migrate")
Flyway flyway() {
Flyway flyway = new Flyway();
flyway.setBaselineOnMigrate(true);
flyway.setDataSource(baseUrl, username, password);
return flyway;
}
问题是怎么做 in Tests ?我想 run the tests against the in-memmory database, 所以我在test / resources中包含了teh application.yml . 但是找不到Flyway beans . 当我从test / resources中删除application.yml时,它运行正常,但是从main / resources中的application.yml读取属性 . 我试图改变测试不是@SpringBootTest,我可以手动创建Flyway bean,但在放入@TestConfiguration之后我需要手动创建所有bean,而不仅仅是Flyway . 何你这样做,只是为了手动创建Flyway bean而所有其余的都留给Spring Injection?
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class SomeTest {
@Autowired
private SomeService someService;
@Test
public void test(){
String helloString = someService.hello();
}}
演示项目可以在这里找到:https://github.com/troger19/demo.git
1 回答
您可以使用
flyway-test-extensions
这是一个来自Flyway的库,旨在允许在Spring中针对Flyway管理的数据库进行测试,您可以查看项目的doc here以及一些用法示例 .