Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ 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
associated with the test class). If you want to publish report entries for the test
class, you can implement a class-level lifecycle method (e.g., `@BeforeAll`) and inject
`TestReporter` into that method.

[[release-notes-5.12.0-M1-junit-jupiter-new-features-and-improvements]]
==== New Features and Improvements
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 @@ -45,6 +45,11 @@ class RepetitionExtension implements ParameterResolver, TestWatcher, ExecutionCo
this.repetitionInfo = repetitionInfo;
}

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

@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
return (parameterContext.getParameter().getType() == RepetitionInfo.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
*/
class TestInfoParameterResolver implements ParameterResolver {

@Override
public ExtensionContextScope getTestInstantiationExtensionContextScope(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 @@ -22,6 +22,11 @@
*/
class TestReporterParameterResolver implements ParameterResolver {

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

@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
return (parameterContext.getParameter().getType() == TestReporter.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class TimeoutExtension implements BeforeAllCallback, BeforeEachCallback, Invocat
private static final String DISABLED_MODE_VALUE = "disabled";
private static final String DISABLED_ON_DEBUG_MODE_VALUE = "disabled_on_debug";

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

@Override
public void beforeAll(ExtensionContext context) {
readAndStoreTimeoutSoChildrenInheritIt(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class ParameterizedTestParameterResolver implements ParameterResolver, AfterTest
this.invocationIndex = invocationIndex;
}

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

@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
Executable declaringExecutable = parameterContext.getDeclaringExecutable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,53 @@

import static java.util.Collections.emptyMap;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.engine.Constants.DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request;

import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.TestReporter;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.platform.commons.PreconditionViolationException;

/**
* @since 5.0
*/
class ReportingTests extends AbstractJupiterTestEngineTests {

@Test
void reportEntriesArePublished() {
executeTestsForClass(MyReportingTestCase.class).testEvents().assertStatistics(stats -> stats //
.started(2) //
.succeeded(2) //
.failed(0) //
.reportingEntryPublished(7));
@ParameterizedTest
@CsvSource(textBlock = """
PER_CLASS, 7
PER_METHOD, 9
""")
void reportEntriesArePublished(Lifecycle lifecycle, int expectedReportEntryCount) {
var request = request() //
.selectors(selectClass(MyReportingTestCase.class)) //
.configurationParameter(DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME, lifecycle.name());
executeTests(request) //
.testEvents() //
.assertStatistics(stats -> stats //
.started(2) //
.succeeded(2) //
.failed(0) //
.reportingEntryPublished(expectedReportEntryCount));
}

@SuppressWarnings("JUnitMalformedDeclaration")
static class MyReportingTestCase {

public MyReportingTestCase(TestReporter reporter) {
// Reported on class-level for PER_CLASS lifecycle and on method-level for PER_METHOD lifecycle
reporter.publishEntry("Constructor");
}

@BeforeEach
void beforeEach(TestReporter reporter) {
reporter.publishEntry("@BeforeEach");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.engine.Constants.DEFAULT_PARALLEL_EXECUTION_MODE;
import static org.junit.jupiter.engine.Constants.PARALLEL_CONFIG_FIXED_PARALLELISM_PROPERTY_NAME;
Expand Down Expand Up @@ -120,6 +121,11 @@ static void afterAll() {
assertEquals(42, fortyTwo);
}

// Can be injected into test class constructors if the test class only has @RepeatedTest methods
public LifecycleMethodTests(RepetitionInfo repetitionInfo) {
assertNotNull(repetitionInfo);
}

@BeforeEach
@AfterEach
void beforeAndAfterEach(TestInfo testInfo, RepetitionInfo repetitionInfo) {
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