Skip to content

Commit 4ed43a6

Browse files
xlorneclaude
andcommitted
fix: 修复 data-fast 测试跨类数据泄漏导致的 CI 外键约束失败
UserRepositoryTest 保存 user→profile→demo 外键链数据后从不清理, 当测试类执行顺序使 UserRepositoryTest 先于 DemoRepositoryTest/ JdbcQueryTest 运行时(执行顺序随 JDK 构建不同而变化,本地与 CI 不一致),后者的 deleteAll 会因 PROFILE 表残留行触发外键约束冲突, 导致 CI 间歇性失败。为 UserRepositoryTest 增加 @BeforeEach/@AfterEach 按外键顺序(user→profile→demo)清理,使其完全自包含。 Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 2264954 commit 4ed43a6

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

springboot-starter-data-fast/src/test/java/com/codingapi/springboot/fast/UserRepositoryTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.codingapi.springboot.fast.repository.UserRepository;
1111
import com.codingapi.springboot.framework.dto.request.PageRequest;
1212
import lombok.extern.slf4j.Slf4j;
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.BeforeEach;
1315
import org.junit.jupiter.api.Test;
1416
import org.springframework.beans.factory.annotation.Autowired;
1517
import org.springframework.boot.test.context.SpringBootTest;
@@ -32,6 +34,27 @@ public class UserRepositoryTest {
3234
@Autowired
3335
private ProfileRepository profileRepository;
3436

37+
/**
38+
* 测试数据与其他测试类共享同一个 H2 库(user→profile→demo 存在外键链),
39+
* 必须按外键顺序清理,且前后都要清理,避免测试类执行顺序不同导致
40+
* DemoRepositoryTest/JdbcQueryTest 的 deleteAll 触发外键约束冲突。
41+
*/
42+
@BeforeEach
43+
void cleanBefore() {
44+
cleanAll();
45+
}
46+
47+
@AfterEach
48+
void cleanAfter() {
49+
cleanAll();
50+
}
51+
52+
private void cleanAll() {
53+
userRepository.deleteAll();
54+
profileRepository.deleteAll();
55+
demoRepository.deleteAll();
56+
}
57+
3558

3659
@Test
3760
void test1() {

0 commit comments

Comments
 (0)