Junit5 重新构造了junit5,支持在代码中执行测试类
使用LauncherDiscoveryRequest、Launcher的方法实现
public void executeByJunit5(String serviceName, String branch, String fileName) {
//需要执行的测试类绝对路径
String testCaseFileAbsPath = "/opt/testcases/" + fileName + ".class";
// 被测服务当前branch dependencies lib目录
String branchLibPath = "/opt/libs/clustera-1-2-3";
try {
//新建classloader
ClassLoader loader = ClassLoaderUtil.createClassLoader("/opt/common/lib");
Collection<File> jars = FileUtils.listFiles(new File(branchLibPath), new String[]{"jar"}, true);
for (File f : jars) {
if (!f.getName().contains("sources")) {
String dependentJar = f.getCanonicalPath();
ClassLoaderUtil.addPath(loader, new File(dependentJar).toURI().toURL());
}
}
// 测试类加到classloader上
ClassLoaderUtil.addPath(loader, new File("/opt/testcases/").toURI().toURL());
// 设置当前进程的classloader为新建的loader
Thread.currentThread().setContextClassLoader(loader);
// 测试类
Class testCaseClazz = loader.loadClass(fileName);
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder
.request()
.selectors(selectClass(testCaseClazz)).build();
Launcher launcher = LauncherFactory.create();
// Register a listener of your choice
SummaryGeneratingListener listener = new SummaryGeneratingListener();
launcher.registerTestExecutionListeners(listener);
launcher.execute(request);
TestExecutionSummary summary = listener.getSummary();
// 报告看情况入库还是直接展示
loggerHelper.info(summary.toString());
} catch (Exception e) {
e.printStackTrace();
loggerHelper.error(e.getStackTrace().toString());
}
}
对应的pom.xml
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.5.2</version>
</dependency>
使用EngineTestKit执行测试类
public void executeByJunit5(String serviceName, String branch, String fileName) {
//需要执行的测试类绝对路径
String testCaseFileAbsPath = "/opt/testcases/" + fileName + ".class";
// 被测服务当前branch dependencies lib目录
String branchLibPath = "/opt/libs/clustera-1-2-3";
try {
//新建classloader
ClassLoader loader = ClassLoaderUtil.createClassLoader("/opt/common/lib");
Collection<File> jars = FileUtils.listFiles(new File(branchLibPath), new String[]{"jar"}, true);
for (File f : jars) {
if (!f.getName().contains("sources")) {
String dependentJar = f.getCanonicalPath();
ClassLoaderUtil.addPath(loader, new File(dependentJar).toURI().toURL());
}
}
// 测试类加到classloader上
ClassLoaderUtil.addPath(loader, new File("/opt/testcases/").toURI().toURL());
// 设置当前进程的classloader为新建的loader
Thread.currentThread().setContextClassLoader(loader);
// 测试类
Class testCaseClazz = loader.loadClass(fileName);
// 这种方法不会生成报告
EngineTestKit
.engine("junit-jupiter")
.selectors(selectClass(testCaseClazz))
.execute();
} catch (Exception e) {
e.printStackTrace();
loggerHelper.error(e.getStackTrace().toString());
}
}
pom.xml除了上面的依赖外,还需要增加
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-testkit</artifactId>
<version>1.5.1</version>
</dependency>
Q&A
Q: 如果在编译测“警告: 未知的枚举常量 Status.STABLE 原因: 找不到org.apiguardian.api.API$Status的类文件”
A: claspath中缺少 apiguardian-api-1.1.0.jar
参考资料:
JUnit 5 User Guide https://goodjob.n2sm.net/cache/?docId=61b8421cac86430996e043365854d253&hq=Java
Junit5学习笔记 https://vitzhou.gitbooks.io/junit5/junit/advanced_topics.html
JUnit 5 Maven Dependency https://howtodoinjava.com/junit5/junit5-maven-dependency/
6.1.1. Discovering Tests https://junit.org/junit5/docs/current/user-guide/#launcher-api-discovery