Merge remote-tracking branch 'origin/main'
This commit is contained in:
181
data/src/test/java/com/sdm/data/InteractiveTestRunner.java
Normal file
181
data/src/test/java/com/sdm/data/InteractiveTestRunner.java
Normal file
@@ -0,0 +1,181 @@
|
||||
package com.sdm.data;
|
||||
|
||||
import com.sdm.data.dao.MapperCompatibilityTest;
|
||||
import com.sdm.data.dao.PostgreSQLCompatibilityTest;
|
||||
import com.sdm.data.service.ServiceCrudTest;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 交互式测试运行器
|
||||
* 启动一次Spring容器后,可以反复执行不同的测试方法
|
||||
*
|
||||
* 使用方法:直接运行此类的main方法,然后在控制台输入测试方法编号
|
||||
*/
|
||||
public class InteractiveTestRunner {
|
||||
|
||||
private static ConfigurableApplicationContext context;
|
||||
private static final Map<String, TestMethodInfo> testMethods = new LinkedHashMap<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("========================================");
|
||||
System.out.println(" 交互式测试运行器 - 启动Spring容器");
|
||||
System.out.println("========================================");
|
||||
|
||||
// 启动Spring容器
|
||||
System.setProperty("spring.profiles.active", "test");
|
||||
context = SpringApplication.run(DataApplication.class, args);
|
||||
|
||||
System.out.println("\n✓ Spring容器启动成功!\n");
|
||||
|
||||
// 注册测试类和方法
|
||||
registerTestClass(MapperCompatibilityTest.class);
|
||||
registerTestClass(ServiceCrudTest.class);
|
||||
registerTestClass(PostgreSQLCompatibilityTest.class);
|
||||
|
||||
// 交互式运行
|
||||
runInteractively();
|
||||
}
|
||||
|
||||
private static void registerTestClass(Class<?> testClass) {
|
||||
for (Method method : testClass.getDeclaredMethods()) {
|
||||
if (method.isAnnotationPresent(org.junit.jupiter.api.Test.class)) {
|
||||
String key = testClass.getSimpleName() + "." + method.getName();
|
||||
testMethods.put(key, new TestMethodInfo(testClass, method));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void runInteractively() {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
while (true) {
|
||||
printMenu();
|
||||
System.out.print("\n请输入编号 (q退出, a运行全部): ");
|
||||
String input = scanner.nextLine().trim();
|
||||
|
||||
if ("q".equalsIgnoreCase(input)) {
|
||||
System.out.println("退出测试...");
|
||||
context.close();
|
||||
break;
|
||||
}
|
||||
|
||||
if ("a".equalsIgnoreCase(input)) {
|
||||
runAllTests();
|
||||
continue;
|
||||
}
|
||||
|
||||
if ("l".equalsIgnoreCase(input)) {
|
||||
continue; // 重新显示菜单
|
||||
}
|
||||
|
||||
try {
|
||||
int index = Integer.parseInt(input);
|
||||
runTestByIndex(index);
|
||||
} catch (NumberFormatException e) {
|
||||
// 尝试按方法名匹配
|
||||
runTestByName(input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void printMenu() {
|
||||
System.out.println("\n========== 可用测试方法 ==========");
|
||||
int index = 1;
|
||||
String currentClass = "";
|
||||
for (Map.Entry<String, TestMethodInfo> entry : testMethods.entrySet()) {
|
||||
String className = entry.getValue().testClass.getSimpleName();
|
||||
if (!className.equals(currentClass)) {
|
||||
currentClass = className;
|
||||
System.out.println("\n【" + className + "】");
|
||||
}
|
||||
System.out.printf(" %3d. %s%n", index++, entry.getValue().method.getName());
|
||||
}
|
||||
System.out.println("\n==================================");
|
||||
System.out.println("命令: [编号]执行单个 | [a]执行全部 | [l]列表 | [q]退出");
|
||||
}
|
||||
|
||||
private static void runTestByIndex(int index) {
|
||||
if (index < 1 || index > testMethods.size()) {
|
||||
System.out.println("❌ 无效编号: " + index);
|
||||
return;
|
||||
}
|
||||
|
||||
int i = 1;
|
||||
for (TestMethodInfo info : testMethods.values()) {
|
||||
if (i++ == index) {
|
||||
runTest(info);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void runTestByName(String name) {
|
||||
for (Map.Entry<String, TestMethodInfo> entry : testMethods.entrySet()) {
|
||||
if (entry.getKey().contains(name) || entry.getValue().method.getName().contains(name)) {
|
||||
runTest(entry.getValue());
|
||||
return;
|
||||
}
|
||||
}
|
||||
System.out.println("❌ 未找到匹配的测试方法: " + name);
|
||||
}
|
||||
|
||||
private static void runTest(TestMethodInfo info) {
|
||||
System.out.println("\n▶ 运行: " + info.testClass.getSimpleName() + "." + info.method.getName());
|
||||
System.out.println("----------------------------------------");
|
||||
|
||||
try {
|
||||
// 从Spring容器获取测试实例
|
||||
Object testInstance = context.getBean(info.testClass);
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
info.method.invoke(testInstance);
|
||||
long duration = System.currentTimeMillis() - start;
|
||||
|
||||
System.out.println("----------------------------------------");
|
||||
System.out.printf("✓ 测试通过! (耗时: %dms)%n", duration);
|
||||
} catch (Exception e) {
|
||||
System.out.println("----------------------------------------");
|
||||
System.out.println("✗ 测试失败!");
|
||||
Throwable cause = e.getCause() != null ? e.getCause() : e;
|
||||
System.out.println("错误: " + cause.getMessage());
|
||||
cause.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void runAllTests() {
|
||||
System.out.println("\n▶ 运行全部测试...");
|
||||
int passed = 0, failed = 0;
|
||||
long totalStart = System.currentTimeMillis();
|
||||
|
||||
for (TestMethodInfo info : testMethods.values()) {
|
||||
System.out.print(" " + info.method.getName() + " ... ");
|
||||
try {
|
||||
Object testInstance = context.getBean(info.testClass);
|
||||
info.method.invoke(testInstance);
|
||||
System.out.println("✓");
|
||||
passed++;
|
||||
} catch (Exception e) {
|
||||
System.out.println("✗");
|
||||
failed++;
|
||||
}
|
||||
}
|
||||
|
||||
long totalDuration = System.currentTimeMillis() - totalStart;
|
||||
System.out.println("\n========================================");
|
||||
System.out.printf("总计: %d 通过, %d 失败 (耗时: %dms)%n", passed, failed, totalDuration);
|
||||
}
|
||||
|
||||
private static class TestMethodInfo {
|
||||
Class<?> testClass;
|
||||
Method method;
|
||||
|
||||
TestMethodInfo(Class<?> testClass, Method method) {
|
||||
this.testClass = testClass;
|
||||
this.method = method;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -23,6 +24,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
* Mapper层兼容性测试 - MySQL转PostgreSQL验证
|
||||
* 覆盖所有Mapper接口的基础CRUD和自定义SQL方法
|
||||
*/
|
||||
@Component // 让测试类成为Spring Bean,支持交互式运行
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -30,6 +31,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
* 4. Boolean字段处理
|
||||
* 5. 字段名大小写敏感性
|
||||
*/
|
||||
@Component // 让测试类成为Spring Bean,支持交互式运行
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -21,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
* Service层CRUD测试 - MySQL转PostgreSQL验证
|
||||
* 覆盖所有Service的基础CRUD操作
|
||||
*/
|
||||
@Component // 让测试类成为Spring Bean,支持交互式运行
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
|
||||
Reference in New Issue
Block a user