Skip to content

Commit

Permalink
Use new scope in TestInfoParameterResolver
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
marcphilipp committed Oct 10, 2024
1 parent 8b9c314 commit 108047c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ JUnit repository on GitHub.
[[release-notes-5.12.0-M1-junit-jupiter-deprecations-and-breaking-changes]]
==== Deprecations and Breaking Changes

* 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). If you require the `TestInfo` of the test class, you can implement a class-level
lifecycle method (e.g., `@BeforeAll`) and inject `TestInfo` into that method.
* When injecting `TestReporter` into test class constructors the published report entries
are now associated with the test method rather than the test class unless the test
instance lifecycle is set to `PER_CLASS` (in which case they will continue to be
Expand Down
4 changes: 2 additions & 2 deletions documentation/src/docs/asciidoc/user-guide/writing-tests.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1058,8 +1058,8 @@ There are currently three built-in resolvers that are registered automatically.
method, or a custom name configured via `@DisplayName`.
+
`{TestInfo}` acts as a drop-in replacement for the `TestName` rule from JUnit 4. The
following demonstrates how to have `TestInfo` injected into a test constructor,
`@BeforeEach` method, and `@Test` method.
following demonstrates how to have `TestInfo` injected into a `@BeforeAll` method, test
class constructor, `@BeforeEach` method, and `@Test` method.

[source,java,indent=0]
----
Expand Down
9 changes: 8 additions & 1 deletion documentation/src/test/java/example/TestInfoDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
Expand All @@ -23,10 +24,16 @@
@DisplayName("TestInfo Demo")
class TestInfoDemo {

TestInfoDemo(TestInfo testInfo) {
@BeforeAll
static void beforeAll(TestInfo testInfo) {
assertEquals("TestInfo Demo", testInfo.getDisplayName());
}

TestInfoDemo(TestInfo testInfo) {
String displayName = testInfo.getDisplayName();
assertTrue(displayName.equals("TEST 1") || displayName.equals("test2()"));
}

@BeforeEach
void init(TestInfo testInfo) {
String displayName = testInfo.getDisplayName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
*/
class TestInfoParameterResolver implements ParameterResolver {

@Override
public ExtensionContextScope getExtensionContextScopeDuringTestClassInstanceConstruction(
ExtensionContext rootContext) {
return ExtensionContextScope.TEST_METHOD;
}

@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
return (parameterContext.getParameter().getType() == TestInfo.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@
@Tag("class-tag")
class TestInfoParameterResolverTests {

private static List<String> allDisplayNames = Arrays.asList("defaultDisplayName(TestInfo)", "custom display name",
"getTags(TestInfo)", "customDisplayNameThatIsEmpty(TestInfo)");
private static final List<String> allDisplayNames = Arrays.asList("defaultDisplayName(TestInfo)",
"custom display name", "getTags(TestInfo)", "customDisplayNameThatIsEmpty(TestInfo)");

public TestInfoParameterResolverTests(TestInfo testInfo) {
assertThat(testInfo.getTestClass()).contains(TestInfoParameterResolverTests.class);
assertThat(testInfo.getTestMethod()).isPresent();
}

@Test
void defaultDisplayName(TestInfo testInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,20 +404,20 @@ void executesLifecycleMethods() {
assertThat(LifecycleTestCase.lifecycleEvents).containsExactly(
"beforeAll:ParameterizedTestIntegrationTests$LifecycleTestCase",
"providerMethod",
"constructor:ParameterizedTestIntegrationTests$LifecycleTestCase",
"constructor:[1] argument=foo",
"beforeEach:[1] argument=foo",
testMethods.get(0) + ":[1] argument=foo",
"afterEach:[1] argument=foo",
"constructor:ParameterizedTestIntegrationTests$LifecycleTestCase",
"constructor:[2] argument=bar",
"beforeEach:[2] argument=bar",
testMethods.get(0) + ":[2] argument=bar",
"afterEach:[2] argument=bar",
"providerMethod",
"constructor:ParameterizedTestIntegrationTests$LifecycleTestCase",
"constructor:[1] argument=foo",
"beforeEach:[1] argument=foo",
testMethods.get(1) + ":[1] argument=foo",
"afterEach:[1] argument=foo",
"constructor:ParameterizedTestIntegrationTests$LifecycleTestCase",
"constructor:[2] argument=bar",
"beforeEach:[2] argument=bar",
testMethods.get(1) + ":[2] argument=bar",
"afterEach:[2] argument=bar",
Expand Down

0 comments on commit 108047c

Please sign in to comment.