在运行单元测试时从application.yml进行配置

时间:2019-01-21 10:22:57

标签: java spring spring-boot flyway spring-boot-test

我从Spring Boot运行应用程序,并读取了用于检查服务方法的单元测试。但出现此错误,尽管我在resources> application.yml中定义了flyway配置,但flyway不存在配置目录。

spring:
 flyway:
   baseline-version: 0
   baseline-description: descript this
   encoding: UTF-8
   table: flyway
   locations: **classpath:/db/postgres/migrations**
   schemas:  SCHEMA
   password: PASSWORD
   user: jeus

但我遇到此错误

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flyway' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.flywaydb.core.Flyway]: Factory method 'flyway' threw exception; nested exception is java.lang.IllegalStateException: Cannot find migrations location in: [classpath:db/migration] (please add migrations or check your Flyway configuration)

在测试类中,我设置了此配置以加载spring boot配置和上下文。

@RunWith(SpringRunner.class)
@ComponentScan
@ContextConfiguration(classes = {BeanFactory4Test.class})
public class ClientManagerTest {

   @Autowired
   private ClientManager clientManager;

   @Test
   public void getAccessTokenTest() {
      ......
   }
}

和BeanFactoryForTest

@Configuration
@ComponentScan(basePackages = {"com.b2mark"}) 
public class BeanFactory4Test {
    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource driver = new DriverManagerDataSource();
        driver.setDriverClassName("org.postgresql.Driver");
        driver.setUrl("jdbc:postgresql://postgres:5432/jeus");
        driver.setUsername("JEUS");
        driver.setPassword("PASSWORD");
        driver.setSchema("SCHEMA");
        return driver;
    }
}

0 个答案:

没有答案