您有多种选择:
JUnit 4 运行监听器
在 JUnit 4 上,您可以注册一个 RunListener,就像 @nrainer 说的那样。如果你使用 Maven 构建,很容易像这样注册一个运行监听器:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<includes>
<include>org.acme.TestSuite</include>
</includes>
<properties>
<property>
<name>listener</name>
<value>org.acme.SuiteRunListener</value>
</property>
</properties>
</configuration>
</plugin>
运行侦听器可以覆盖事件testSuiteStarted 和testSuiteFinished,并直接记录您感兴趣的注释或将它们分配给testSuiteStarted 中的private static ThreadLocal<List<Annotation>> currentSuiteAnnotations 等静态线程局部变量,然后再次取消分配在testSuiteFinished。
这在 Maven 中运行良好,我对其进行了测试。不幸的是,没有直接支持使用来自 IntelliJ IDEA 或 Eclipse 等 IDE 的运行侦听器运行测试。因此,如果您想避免从具有 main 方法的类手动运行测试,如 here 所示,因为它会带走所有漂亮的 IDE 测试报告,从套件到测试类再到测试方法,这不是一个选项。
JUnit 5 测试执行监听器
类似于 JUnit 4 的运行监听器,您可以为您的 JUnit 5 测试注册一个TestExecutionListener。 JUnit 5 的优点是您可以通过 Java 的 ServiceLoader 机制在全局范围内注册它,即它会在引导 JUnit 时被拾取,并且也应该在 IDE 中工作。我用另一种类型的扩展做了类似的事情,它在 IntelliJ IDEA 中运行良好,当然也在 Maven 中。
带有自定义套件运行器的 JUnit 4
回到 JUnit 4,我们可以通过声明一种特殊类型的套件来扩展第一种方法与运行侦听器。您只需使用该套件而不是 org.junit.runners.Suite 即可享受 Maven 和 IDE 中的运行监听器。它是这样工作的,为方便起见,另请参阅我的MCVE on GitHub:
package org.acme;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runner.notification.RunListener;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import java.lang.annotation.Annotation;
import java.util.List;
import java.util.stream.Collectors;
public class SuiteRunListener extends RunListener {
private static ThreadLocal<String> currentSuiteName = new ThreadLocal<String>();
private static ThreadLocal<List<Annotation>> currentSuiteAnnotations = new ThreadLocal<>();
@Override
public void testSuiteStarted(Description description) throws Exception {
super.testSuiteStarted(description);
final RunWith runWith = description.getAnnotation(RunWith.class);
if (runWith != null && runWith.value().equals(SuiteWithListener.class)) {
currentSuiteName.set(description.getDisplayName());
currentSuiteAnnotations.set(
description.getAnnotations().stream()
.filter(annotation -> {
final Class<? extends Annotation> annotationType = annotation.annotationType();
return !(annotationType.equals(RunWith.class) || annotationType.equals(SuiteClasses.class));
})
.collect(Collectors.toList())
);
}
}
@Override
public void testSuiteFinished(Description description) throws Exception {
super.testSuiteFinished(description);
final RunWith runWith = description.getAnnotation(RunWith.class);
if (runWith != null && runWith.value().equals(SuiteWithListener.class)) {
currentSuiteName.set(null);
currentSuiteAnnotations.set(null);
}
}
public static String getCurrentSuiteName() {
return currentSuiteName.get();
}
public static List<Annotation> getCurrentSuiteAnnotations() {
return currentSuiteAnnotations.get();
}
}
package org.acme;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.Suite;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;
import java.util.List;
public class SuiteWithListener extends Suite {
public SuiteWithListener(Class<?> klass, RunnerBuilder builder) throws InitializationError {
super(klass, builder);
}
public SuiteWithListener(RunnerBuilder builder, Class<?>[] classes) throws InitializationError {
super(builder, classes);
}
protected SuiteWithListener(Class<?> klass, Class<?>[] suiteClasses) throws InitializationError {
super(klass, suiteClasses);
}
protected SuiteWithListener(RunnerBuilder builder, Class<?> klass, Class<?>[] suiteClasses) throws InitializationError {
super(builder, klass, suiteClasses);
}
protected SuiteWithListener(Class<?> klass, List<Runner> runners) throws InitializationError {
super(klass, runners);
}
@Override
public void run(RunNotifier notifier) {
notifier.addListener(new SuiteRunListener()); // !!!
super.run(notifier);
}
}
package org.acme;
import org.junit.runner.RunWith;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(SuiteWithListener.class) // !!!
@SuiteClasses({
FirstTest.class,
SecondTest.class
})
@TestSuiteAnnotation
public class TestSuite {}
package org.acme;
import org.junit.Before;
import java.util.Arrays;
public class ExtTest {
@Before
public void beforeMethod() {
String currentSuiteName = SuiteRunListener.getCurrentSuiteName();
if (currentSuiteName != null) {
System.out.println("Annotations from suite " + currentSuiteName);
SuiteRunListener.getCurrentSuiteAnnotations().forEach(System.out::println);
}
System.out.println("Annotations from class " + this.getClass());
Arrays.asList(this.getClass().getAnnotations()).forEach(System.out::println);
System.out.println();
}
}
现在在运行您的套件时,您应该会看到如下输出:
Annotations from suite org.acme.TestSuite
@org.acme.TestSuiteAnnotation()
Annotations from class class org.acme.FirstTest
@org.acme.FirstAnnotation()
Annotations from suite org.acme.TestSuite
@org.acme.TestSuiteAnnotation()
Annotations from class class org.acme.SecondTest
@org.acme.SecondAnnotation()
请注意:我假设您确实需要从每个测试方法访问当前套件,而不仅仅是在测试类或套件级别。如果您不需要它并且在套件启动和/或完成时让运行侦听器执行某些操作就足够了,那么您当然不需要当前套件名称和套件注释的 getter 方法。我只是扩展了你自己的例子。