Spring Boot + Testcontainers + Liquibase: getting used DB during tests leads to failing tests

264 Views Asked by At

first up: the setup:

  1. Spring Boot 3.1.5
  2. JUnit 5 testing environment
  3. Testcontainers v. 1.19.1
  4. switched DB-Url in application-test.yml to jdbc:tc:postgresql:15.1:///integration-tests-db (as described here: https://java.testcontainers.org/modules/databases/jdbc/)

I'm trying to use Testcontainers for my integration tests. For each test (not just just class - @Test annotated method), the DB needs to be clean, no tables, no indices, no sequences: empty. We're using Liquibase to setup the DB.

But somehow, the DB gets shared between multiple tests, actually: between multiple test classes.

As a result, Liquibase sees no reason to add the table - they are there anyway. Which is also true for sequences, which is an issue during my tests. And finally, I would expect 2 results during my test, but I find more, which are leftovers from previous tests.

Here's the Liquibase result from starting the first test class:

@ExtendWith(SpringExtension.class)
@ActiveProfiles("test")
@SpringBootTest
@TestPropertySource(properties = "spring.main.allow-bean-definition-overriding=true")
class AuditResultServiceTest {
    @Autowired
    private AuditResultService auditResultService;
    
    [... some more @Autowired attributes used by the test setup ...]
    @Test
    void canLoadAuditResult() { [... here we go ...] }

    @ComponentScan(basePackages = "com.application")
    @EnableJpaRepositories(basePackages = "com.application")
    @EntityScan(basePackages = "com.application")
    @SpringBootApplication
    @PropertySource("classpath:application-test.yml")
    static class TestConfiguration {}
}

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.1.5)

[... some Hikari and Liquibase log messages ...]

11412 [main] INFO  liquibase.util - Total change sets:          102
 

UPDATE SUMMARY
Run:                        102
Previously run:               0
Filtered out:                 0
-------------------------------
Total change sets:          102

And here's the Liquibase result from starting the second test class - that's not even on the expected test method level, but on class level:

@ExtendWith(SpringExtension.class)
@ActiveProfiles("test")
@SpringBootTest
@TestPropertySource(properties = "spring.main.allow-bean-definition-overriding=true")
class AuditCheckerServiceTest {
    @Autowired
    private AuditCheckerService auditResultService;
    
    [... some more @Autowired attributes used by the test setup ...]
    @Test
    void canCheckAudit() { [... here we go ...] }

    @ComponentScan(basePackages = "com.application")
    @EnableJpaRepositories(basePackages = "com.application")
    @EntityScan(basePackages = "com.application")
    @SpringBootApplication
    @PropertySource("classpath:application-test.yml")
    static class TestConfiguration {}
}


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.1.5)

[... some Hikari and Liquibase log messages ...]

11412 [main] INFO  liquibase.util - Total change sets:          102
 

UPDATE SUMMARY
Run:                          0
Previously run:             102
Filtered out:                 0
-------------------------------
Total change sets:          102

Is my approach wrong, in the sense that Testcontainers does not support the scenario of providing a new, isolated DB to each test method? Or what am I missing in my settings? My test classes did not get changed when switching from a working H2 setup to Testcontainers.

Thanks a lot.

0

There are 0 best solutions below