flyway / flyway-test-extensions

Apache License 2.0
129 stars 35 forks source link

@FlywayTest with @BeforeEach doesn't clear DB #87

Closed Rafal-Laskowski closed 4 years ago

Rafal-Laskowski commented 4 years ago

Hello,

I try to implement FlywayTest extension with Spring 5 and JUnit 5.

What I was able to achieve:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles(profiles = "db")
@ExtendWith(FlywayTestExtension.class)
public class IntegrationTest {

If I annotate the method with @FlywayTest the database is clear.

However, if I do it on beforeEach level, DB is not cleared:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles(profiles = "db")
@ExtendWith(FlywayTestExtension.class)
public class IntegrationTest {

    @BeforeEach
    @FlywayTest
    void flywayClean() {

    }

The above code is full example. I do not use any other methods or annotations. Do I do something wrong? Am I missing something?

Thank you in advance

Rafal-Laskowski commented 4 years ago

Okay, I actually found the solution.

Working example:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles(profiles = "db")
@TestExecutionListeners({FlywayTestExecutionListener.class, DependencyInjectionTestExecutionListener.class})
public class IntegrationTest {

    @BeforeEach
    @FlywayTest
    void flywayClean() {

    }

Use FlywayTestExecutionListener so it can invoke the annotation on @BeforeEach method and add DependencyInjectionTestExecutionLicenser.class if you use any Spring values like @LocalServerPort