【发布时间】:2019-05-10 13:02:28
【问题描述】:
由于旧的 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;
}
问题是如何在测试中做到这一点?我想针对内存数据库运行测试,因此我将 application.yml 包含在测试/资源中。但是随后找不到 Flyway bean。当我从 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
【问题讨论】:
标签: java spring-boot flyway