4

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!

asked Oct 11, 2022 at 19:18
4
  • you have disabled liquibase by property spring.liquibase.enabled=false is that what you want? Commented Oct 12, 2022 at 11:07
  • I edited the question, and made spring.liquibase.enabled=true. But the result is same, cannot get the data by providing employeeId. Commented Oct 12, 2022 at 14:43
  • could you share an example somewhere on github? Commented Oct 13, 2022 at 15:02
  • here- github.com/AbhinashJha342/employee Commented Oct 13, 2022 at 15:18

1 Answer 1

2

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)

answered Oct 14, 2022 at 4:53
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.