我正在嘗試使用 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());
}
應用程式-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
總結一下:
- 我想使用employee_id- cc95ccff-8169-4559-9806-1ca4a1db3a19 查詢資料庫,因為我(認為)將其插入到當前不回傳資料的db。
謝謝您的幫助!
uj5u.com熱心網友回復:
Liquibase 已被用于創建執行腳本。出于這個原因,以下屬性應該是spring.jpa.hibernate.ddl-auto=noneatapplication-tests.properties以覆寫application.properties.
我已經看到你在提供的代碼中修復了,但是@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/515687.html
