jpa SpringBoot+Liquibase+TestContainer数据库-集成测试期间无法填充数据库

roejwanj  于 2022-11-14  发布在  Spring
关注(0)|答案(1)|浏览(130)

我尝试使用TestContainers进行集成测试。我尝试使用sql脚本填充一些数据,然后使用测试添加新数据。下面是测试设置:我尝试通过sql脚本获取插入数据的集成测试:

@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文件夹有2个sql文件,一个创建数据库模式,另一个填充一些已创建的模式。例如:

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);

正如我在日志中看到的,更改日志正在被选中:

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

总结一下:
1.我想使用employe_id-cc 95 ccff-8169-4559-9806- 1ca 4a 1db 3a 19查询数据库,因为我(认为)正在将其插入到db中,而db当前并不返回数据。
谢谢你的帮助!

rqenqsqc

rqenqsqc1#

Liquibase已用于创建执行脚本。因此,以下属性在application-tests.properties处应为spring.jpa.hibernate.ddl-auto=none,以便覆盖application.properties中的属性。
我已经在提供的代码中看到了你的修复,但是@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)

相关问题