Home > Back-end >  SpringBoot Liquibase TestContainer db - not able to populate db during integration test
SpringBoot Liquibase TestContainer db - not able to populate db during integration test

Time:10-15

I am trying to use TestContainers for my integration tests. What I am trying to do is use sql script to populate with some data, and then also add new data using tests. Below is the test setup: Integration test where I am trying to get the data inserted through sql scripts:

@SpringBootTest
@AutoConfigureMockMvc
@ExtendWith(SpringExtension.class)
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
@TestPropertySource(value = {
        "classpath:application-test.properties"
})
class EmployeeDatabaseApplicationTests 

@Test
void getEmployeeByEmployeeId() throws Exception {
    List<UUID> employeeIds = List.of();
    this.mockMvc.perform(get("/admin/employees")
                    .accept(EmployeeProfileUtil.MEDIA_TYPE_JSON_UTF8)
                    .contentType(EmployeeProfileUtil.MEDIA_TYPE_JSON_UTF8)
                    .header("Employee-id", "cc95ccff-8169-4559-9806-1ca4a1db3a19"))
            .andExpect(status().is2xxSuccessful());
}

application-test.properties:

spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver
spring.datasource.url=jdbc:tc:postgresql://employee
spring.jpa.hibernate.ddl-auto = create
spring.liquibase.contexts=test
spring.liquibase.change-log=classpath:/db-test.changelog/db.changelog-test-master.yaml

db.changelog-test-master.yaml

databaseChangeLog:
    - includeAll:
        path: classpath*:db-test.changelog/changes/

changes folder has 2 sql files, one creates the database schema and the other populates some of the created schema. ex:

CREATE TABLE IF NOT EXISTS employee (
                                        id int8 generated by default as identity,
                                        date_of_birth varchar(255),
    deleted boolean not null,
    employee_id uuid,
    gender varchar(255),
    phone varchar(255),
    name_id int8,
    primary key (id)
    );
INSERT INTO employee (id, date_of_birth, deleted, employee_id, gender, phone, name_id) values (1, '2010-02-02', false, 'cc95ccff-8169-4559-9806-1ca4a1db3a19',
'female', '5561132977', 1);

The change log is getting picked as I can see in logs:

liquibase.changelog                      : Custom SQL executed
liquibase.changelog                      : ChangeSet db-test.changelog/changes/employee-create-tables-20220810.sql::raw::includeAll ran successfully in 22ms
liquibase.changelog                      : ChangeSet db-test.changelog/changes/employee-create-tables-20221010.sql::raw::includeAll ran successfully in 6ms

To summarize:

  1. I want to query the database using employe_id- cc95ccff-8169-4559-9806-1ca4a1db3a19, as I(think)am inserting this to db, which currently doesn't return the data.

Thanks for the help!

CodePudding user response:

Liquibase is already being used to create perform the scripts. For that reason the following property should be spring.jpa.hibernate.ddl-auto=none at application-tests.properties in order to override the one in application.properties.

I already saw you fixed in the code provided but @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)

  • Related