0

While running default Spring Boot unit test:

@SpringBootTest
class DemoH2FlywayApplicationTests {
    @Test
    void contextLoads() {
    }
}

I am getting this error:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flyway' defined in class path resource 

I am running Spring Boot 2.6.2, using Flyway and H2, and these dependencies:

spring-boot-starter-data-jdbc
spring-boot-starter-data-jpa
spring-boot-starter-web
flyway-core
spring-boot-devtools
h2
lombok
spring-boot-starter-test

Here is the demo project: https://github.com/filip194/demo-h2-flyway/.

I am not sure, nor expert, but maybe there is some correlation with Hibernate that I can not understand. I have tried to create test resources folder with application.properties for H2, the same as in the main resources, but nothing changes.

Can anybody help?

filip194
  • 3
  • 4
  • when you add flyway dependency its enabled by default, Try adding application.properties file in test resources folder with spring.flyway.enabled=false – MyTwoCents Jan 15 '22 at 05:26
  • Thank you for your answer, but the case is I need flyway to create data in DB tables for test purposes. – filip194 Jan 16 '22 at 09:26

2 Answers2

0

The answer to your question is explained in more detail here.

There is an error in the following line in the application.properties file.

spring.flyway.baseline-on-migrate=true
spring.flyway.validate-on-migrate: false

In the second line you should write = instead of :

  • Thank you for your answer, I have fixed this typo but nothing changed. I've also taken a look at the link you provided, changed the properties as it is suggested, also added the props for #spring.flyway.url #spring.flyway.user: user and #spring.flyway.password. Didn't help, the same error occurs again. One interesting thing to note (I am running this test in IntelliJ), when I run this test in debug mode or in coverage mode, it passes. – filip194 Jan 16 '22 at 09:50
  • `Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Unresolvable class definition; nested exception is java.lang.NoClassDefFoundError: com/intellij/rt/coverage/data/TestDiscoveryProjectData Caused by: java.lang.NoClassDefFoundError: com/intellij/rt/coverage/data/TestDiscoveryProjectData Caused by: java.lang.ClassNotFoundException: com.intellij.rt.coverage.data.TestDiscoveryProjectData` Might be the problem with IntelliJ... – filip194 Jan 16 '22 at 09:51
0

I launched your project in Intellij IDEA 2019.3.3 (Java 14) and Maven terminal. The test was completed successfully and the context was created.

Try running your project using the Maven terminal: mvn clean istall. Install it in your OS if maven is missing.

If everything is successful, then the problem is probably in the Intellij IDEA

Konanaw
  • 41
  • 3
  • Thanks for your answer, that's what I was suspecting. Finally, I got a chance to run this test on my peer's computer and it passed with flying colors, so there is something wrong with IntelliJ on my end. Will do as you said. – filip194 Jan 26 '22 at 10:11