Liquibase not running in spring test

35 Views Asked by At

I have a junit test that requires a database (integration test) using an H2 in-memory DB. The application uses a Postgres db.

I am using Liquibase to configure the DB and that works perfectly creates the tables in Postgres without issue.

As the application boots can see Liquibase action in the logs.

For the test Liquibase never runs. App is spring-boot 3.2.3, Liquibase 4.26.0 test is as follows:

@Test
public void TestDbTableCreated(){
    boolean dbIsJdbc = "JdbcDatabase".equals(database.getName());
    assumeTrue(dbIsJdbc);
    assertEquals( 0, database.countUsers());
    assertTrue(database.registerUser("Fred Bloggs", "[email protected]"));
    assertNotEquals( 0, database.countUsers());
    Optional<Integer> fredsIdByEmail = database.getUserIdByEmail("[email protected]");
    assumeTrue(fredsIdByEmail.isPresent());
    Optional<Integer> fredsIdByName = database.getUserIdByName("Fred Bloggs");
    assumeTrue(fredsIdByName.isPresent());
    assertEquals(fredsIdByEmail.get(), fredsIdByName.get());
}

According to all the stuff I have read Google searching on this, all I should need to do is add Liqubase as a dependency which works fine for running the app but does nothing for the test which fails on database.countUsers() with an error message

Table "USERS" not found (this database is empty); SQL statement:

application.yml

spring:
  profiles:
    active: jdbc
  test:
    database:
      replace=none:
  application:
    name: "Coding Example UNDER_TEST"
  main:
    allow-bean-definition-overriding: true
  thymeleaf:
    cache: false
  datasource:
    url: jdbc:h2:mem:app_example
    username: SA
    password:
    statement-cache-size: 250
    statement-cache-sql-limit: 2048
  liquibase:
    enabled: true
    contextFilter: test
    change-log: classpath:db/changelog/db.changelog-master.yaml
    url: jdbc:h2:mem:app_example;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS public
    user: sa
    password:
    default-schema: public
    drop-first: true

I have made multiple changes to this file after reading many Google posts but nothing changes always fails at the same place with the same error!

Even tried deleting the Liquibase config no effect.

Out of ideas nothing new in the last hour of Googling.

0

There are 0 best solutions below