Skip to content

Commit 108047c

Browse files
committed
Use new scope in TestInfoParameterResolver
When injecting `TestInfo` into test class constructors it now contains data of the test method the test class instance is being created for unless the test instance lifecycle is set to `PER_CLASS` (in which case it continues to contain the data of the test class).
1 parent 8b9c314 commit 108047c

File tree

6 files changed

+32
-9
lines changed

6 files changed

+32
-9
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.12.0-M1.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ JUnit repository on GitHub.
4444
[[release-notes-5.12.0-M1-junit-jupiter-deprecations-and-breaking-changes]]
4545
==== Deprecations and Breaking Changes
4646

47+
* When injecting `TestInfo` into test class constructors it now contains data of the test
48+
method the test class instance is being created for unless the test instance lifecycle
49+
is set to `PER_CLASS` (in which case it continues to contain the data of the test
50+
class). If you require the `TestInfo` of the test class, you can implement a class-level
51+
lifecycle method (e.g., `@BeforeAll`) and inject `TestInfo` into that method.
4752
* When injecting `TestReporter` into test class constructors the published report entries
4853
are now associated with the test method rather than the test class unless the test
4954
instance lifecycle is set to `PER_CLASS` (in which case they will continue to be

documentation/src/docs/asciidoc/user-guide/writing-tests.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,8 +1058,8 @@ There are currently three built-in resolvers that are registered automatically.
10581058
method, or a custom name configured via `@DisplayName`.
10591059
+
10601060
`{TestInfo}` acts as a drop-in replacement for the `TestName` rule from JUnit 4. The
1061-
following demonstrates how to have `TestInfo` injected into a test constructor,
1062-
`@BeforeEach` method, and `@Test` method.
1061+
following demonstrates how to have `TestInfo` injected into a `@BeforeAll` method, test
1062+
class constructor, `@BeforeEach` method, and `@Test` method.
10631063

10641064
[source,java,indent=0]
10651065
----

documentation/src/test/java/example/TestInfoDemo.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import static org.junit.jupiter.api.Assertions.assertEquals;
1515
import static org.junit.jupiter.api.Assertions.assertTrue;
1616

17+
import org.junit.jupiter.api.BeforeAll;
1718
import org.junit.jupiter.api.BeforeEach;
1819
import org.junit.jupiter.api.DisplayName;
1920
import org.junit.jupiter.api.Tag;
@@ -23,10 +24,16 @@
2324
@DisplayName("TestInfo Demo")
2425
class TestInfoDemo {
2526

26-
TestInfoDemo(TestInfo testInfo) {
27+
@BeforeAll
28+
static void beforeAll(TestInfo testInfo) {
2729
assertEquals("TestInfo Demo", testInfo.getDisplayName());
2830
}
2931

32+
TestInfoDemo(TestInfo testInfo) {
33+
String displayName = testInfo.getDisplayName();
34+
assertTrue(displayName.equals("TEST 1") || displayName.equals("test2()"));
35+
}
36+
3037
@BeforeEach
3138
void init(TestInfo testInfo) {
3239
String displayName = testInfo.getDisplayName();

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TestInfoParameterResolver.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
*/
2929
class TestInfoParameterResolver implements ParameterResolver {
3030

31+
@Override
32+
public ExtensionContextScope getExtensionContextScopeDuringTestClassInstanceConstruction(
33+
ExtensionContext rootContext) {
34+
return ExtensionContextScope.TEST_METHOD;
35+
}
36+
3137
@Override
3238
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
3339
return (parameterContext.getParameter().getType() == TestInfo.class);

jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/TestInfoParameterResolverTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@
3535
@Tag("class-tag")
3636
class TestInfoParameterResolverTests {
3737

38-
private static List<String> allDisplayNames = Arrays.asList("defaultDisplayName(TestInfo)", "custom display name",
39-
"getTags(TestInfo)", "customDisplayNameThatIsEmpty(TestInfo)");
38+
private static final List<String> allDisplayNames = Arrays.asList("defaultDisplayName(TestInfo)",
39+
"custom display name", "getTags(TestInfo)", "customDisplayNameThatIsEmpty(TestInfo)");
40+
41+
public TestInfoParameterResolverTests(TestInfo testInfo) {
42+
assertThat(testInfo.getTestClass()).contains(TestInfoParameterResolverTests.class);
43+
assertThat(testInfo.getTestMethod()).isPresent();
44+
}
4045

4146
@Test
4247
void defaultDisplayName(TestInfo testInfo) {

jupiter-tests/src/test/java/org/junit/jupiter/params/ParameterizedTestIntegrationTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,20 +404,20 @@ void executesLifecycleMethods() {
404404
assertThat(LifecycleTestCase.lifecycleEvents).containsExactly(
405405
"beforeAll:ParameterizedTestIntegrationTests$LifecycleTestCase",
406406
"providerMethod",
407-
"constructor:ParameterizedTestIntegrationTests$LifecycleTestCase",
407+
"constructor:[1] argument=foo",
408408
"beforeEach:[1] argument=foo",
409409
testMethods.get(0) + ":[1] argument=foo",
410410
"afterEach:[1] argument=foo",
411-
"constructor:ParameterizedTestIntegrationTests$LifecycleTestCase",
411+
"constructor:[2] argument=bar",
412412
"beforeEach:[2] argument=bar",
413413
testMethods.get(0) + ":[2] argument=bar",
414414
"afterEach:[2] argument=bar",
415415
"providerMethod",
416-
"constructor:ParameterizedTestIntegrationTests$LifecycleTestCase",
416+
"constructor:[1] argument=foo",
417417
"beforeEach:[1] argument=foo",
418418
testMethods.get(1) + ":[1] argument=foo",
419419
"afterEach:[1] argument=foo",
420-
"constructor:ParameterizedTestIntegrationTests$LifecycleTestCase",
420+
"constructor:[2] argument=bar",
421421
"beforeEach:[2] argument=bar",
422422
testMethods.get(1) + ":[2] argument=bar",
423423
"afterEach:[2] argument=bar",

0 commit comments

Comments
 (0)