[英]How to access an annotation value (JUnit 4 -> JUnit 5)
I am trying to port a project from JUnit 4 to JUnit 5. The project includes a custom runner that has a listener that detects whether a test has a certain annotation ( @GradedTest
) and accesses the annotation's key-value pairs.我正在尝试将一个项目从 JUnit 4 移植到 JUnit 5。该项目包括一个自定义运行器,该运行器具有一个侦听器,用于检测测试是否具有特定注释 ( @GradedTest
) 并访问注释的键值对。 For example, it would be able to access the values associated with name
and points
in this code:例如,它将能够访问与此代码中的name
和points
关联的值:
@Test
@GradedTest(name = "greet() test", points = "1")
public void defaultGreeting() {
assertEquals(GREETING, unit.greet());
}
The existing JUnit 4 code has a listener that extends RunListener
and overrides testStarted()
:现有的 JUnit 4 代码有一个监听器,它扩展RunListener
并覆盖testStarted()
:
@Override
public void testStarted(Description description) throws Exception {
super.testStarted(description);
this.currentGradedTestResult = null;
GradedTest gradedTestAnnotation = description.getAnnotation(GradedTest.class);
if (gradedTestAnnotation != null) {
this.currentGradedTestResult = new GradedTestResult(
gradedTestAnnotation.name(),
gradedTestAnnotation.number(),
gradedTestAnnotation.points(),
gradedTestAnnotation.visibility()
);
}
}
Note that this makes use of Description.getAnnotation()
.请注意,这使用了Description.getAnnotation()
。
I am trying to switch to the JUnit Platform Launcher API .我正在尝试切换到JUnit 平台启动器 API 。 I can use a LauncherDiscoveryRequestBuilder
to select the tests I want to run, and I can create listeners that extend SummaryGeneratingListener
and override executionStarted(TestIdentifier testIdentifier)
.我可以使用LauncherDiscoveryRequestBuilder
来 select 我想运行的测试,我可以创建扩展SummaryGeneratingListener
并覆盖executionStarted(TestIdentifier testIdentifier)
的侦听器。 I see no way, however, to get an annotation and its values from a TestIdentifier
.但是,我看不到从TestIdentifier
获取注释及其值的方法。
What is the JUnit 5 equivalent of Description.getAnnotation()
or the new way of getting a test annotation's values? JUnit 5 等同于Description.getAnnotation()
或获取测试注释值的新方法是什么?
I did find a way to get annotations, but I do not know how robust it is.我确实找到了一种获取注释的方法,但我不知道它有多健壮。 This is how I overrode SummaryGeneratingListener.executionStarted(TestIdentifier identifier)
:这就是我如何覆盖SummaryGeneratingListener.executionStarted(TestIdentifier identifier)
:
@Override
public void executionStarted(TestIdentifier identifier) {
super.executionStarted(identifier);
this.currentGradedTestResult = null;
// Check if this is an atomic test, not a container.
if (identifier.isTest()) {
// Check if the test's source is provided.
TestSource source = identifier.getSource().orElse(null);
// If so, and if it's a MethodSource, get and use the annotation if present.
if (source != null && source instanceof MethodSource) {
GradedTest gradedTestAnnotation = ((MethodSource) source).getJavaMethod().getAnnotation(GradedTest.class);
if (gradedTestAnnotation != null) {
this.currentGradedTestResult = new GradedTestResult(
gradedTestAnnotation.name(),
gradedTestAnnotation.number(),
gradedTestAnnotation.points(),
gradedTestAnnotation.visibility()
);
this.currentGradedTestResult.setScore(gradedTestAnnotation.points());
}
}
}
this.testOutput = new ByteArrayOutputStream();
System.setOut(new PrintStream(this.testOutput));
}
The weak link is TestIdentifier.getSource()
.薄弱环节是TestIdentifier.getSource()
。 The documentation says it gets "the source of the represented test or container, if available ."该文档说它获取“所表示的测试或容器的来源,如果可用的话”。 It works for my tests, but I don't know under what circumstances the source is (not) available.它适用于我的测试,但我不知道在什么情况下源(不)可用。
I'm providing this as a solution but not marking this as answered, in case there's a better solution.我将此作为解决方案提供,但不会将其标记为已回答,以防有更好的解决方案。 If anyone is interested, there are the commits .如果有人感兴趣,可以查看 commits 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.