0
votes

So I have two integration test classes. I am using the flyway-test-extension for resetting the db. When I run the test classes individually from IntelliJ, both pass. However, when I run them with mvn clean install or in IntelliJ all test together, TestClass2 fails with an exception.

Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Found non-empty schema(s) "public" without schema history table! Use baseline() or set baselineOnMigrate to true to initialize the schema history table.

I tried setting spring.flyway.baseline-on-migrate=true in the core_test.proberties file, but without success.

Difference between TestClass1 and TestClass2 is that @FlywayTest is once on class level and once on method level. Also in TestClass1 I use @MockBean for some of my services. Might this cause the error?

These are the tests I have:

@SpringBootTest(classes = { CoreTestConfig.class })
@TestPropertySource("classpath:core_test.properties")
@ExtendWith(SpringExtension.class)
@ExtendWith({FlywayTestExtension.class})
@FlywayTest // on class level
class TestClass1 {
    // contains injected mocks @MockBean
}
@SpringBootTest(classes = { CoreTestConfig.class })
@TestPropertySource("classpath:core_test.properties")
@ExtendWith(SpringExtension.class)
@ExtendWith({FlywayTestExtension.class})
class TestClass2 {

    @Test
    @FlywayTest // on method level
    void someTestMethod() {
        // ...
    }

}

and the context configuration for the test as follows:

@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties({
        AppProperties.class
})
@EnableTransactionManagement
@EnableAsync
@EnableScheduling
@Import({ActivitiConfiguration.class})
@ComponentScan(basePackages = { "com.company.product.core" },
        excludeFilters = @ComponentScan.Filter(value = Configuration.class)
)
@EntityScan(basePackageClasses = BaseDO.class)
@EnableJpaRepositories(basePackages = "com.company.product.core.repository")
public class CoreTestConfig {
    // contains some specific test beans
}

I use org.flywaydb.flyway-test-extensions version 6.4.0.

UPDATE: When I remove @MockBean inside TestClass1, there are no more errors when running all tests together. But how am I supposed to use @MockBean with the flyway test extension?

1

1 Answers

0
votes

As shown in this tutorial, mocking the beans inside of the CoreTestConfig class resolved the issue. I am not sure why but it works. And then simply @Autowire for the mocked services inside the tests.