Skip to content

Commit 76065a2

Browse files
committed
Make @testinstance an @inherited annotation
Issue: #419
1 parent fa8dc48 commit 76065a2

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

Diff for: junit-jupiter-api/src/main/java/org/junit/jupiter/api/TestInstance.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import java.lang.annotation.Documented;
1414
import java.lang.annotation.ElementType;
15+
import java.lang.annotation.Inherited;
1516
import java.lang.annotation.Retention;
1617
import java.lang.annotation.RetentionPolicy;
1718
import java.lang.annotation.Target;
@@ -22,13 +23,15 @@
2223
* test class.
2324
*
2425
* <p>If {@code @TestInstance} is not declared on a test class, the lifecycle
25-
* mode will default to {@link Lifecycle#PER_METHOD PER_METHOD}.
26+
* mode will default to {@link Lifecycle#PER_METHOD PER_METHOD}. Note, however,
27+
* that the lifecycle mode is <em>inherited</em> within test class hierarchies.
2628
*
2729
* @author Sam Brannen
2830
* @since 5.0
2931
*/
3032
@Target(ElementType.TYPE)
3133
@Retention(RetentionPolicy.RUNTIME)
34+
@Inherited
3235
@Documented
3336
public @interface TestInstance {
3437

Diff for: junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/TestInstanceLifecycleTests.java

+13-9
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,12 @@
5757
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
5858
import org.junit.platform.commons.util.ReflectionUtils;
5959
import org.junit.platform.engine.test.event.ExecutionEventRecorder;
60-
import org.junit.platform.runner.JUnitPlatform;
61-
import org.junit.runner.RunWith;
6260

6361
/**
6462
* Integration tests for {@link TestInstance @TestInstance} lifecycle support.
6563
*
6664
* @since 5.0
6765
*/
68-
@RunWith(JUnitPlatform.class)
69-
public
70-
7166
class TestInstanceLifecycleTests extends AbstractJupiterTestEngineTests {
7267

7368
private static final Map<String, Object> instanceMap = new LinkedHashMap<>();
@@ -155,7 +150,15 @@ void instancePerMethod() {
155150

156151
@Test
157152
void instancePerClass() {
158-
Class<?> testClass = InstancePerClassTestCase.class;
153+
instancePerClass(InstancePerClassTestCase.class);
154+
}
155+
156+
@Test
157+
void instancePerClassWithInheritedLifecycleMode() {
158+
instancePerClass(SubInstancePerClassTestCase.class);
159+
}
160+
161+
private void instancePerClass(Class<?> testClass) {
159162
int containers = 3;
160163
int tests = 3;
161164
int instances = 1;
@@ -475,8 +478,6 @@ private void performAssertions(Class<?> testClass, int containers, int tests, in
475478

476479
ExecutionEventRecorder eventRecorder = executeTestsForClass(testClass);
477480

478-
// eventRecorder.eventStream().forEach(System.out::println);
479-
480481
// @formatter:off
481482
assertAll(
482483
() -> assertEquals(containers, eventRecorder.getContainerStartedCount(), "# containers started"),
@@ -584,7 +585,7 @@ static void afterAllStatic(TestInfo testInfo) {
584585
}
585586

586587
@TestInstance(Lifecycle.PER_CLASS)
587-
private static class InstancePerClassTestCase extends InstancePerMethodTestCase {
588+
static class InstancePerClassTestCase extends InstancePerMethodTestCase {
588589

589590
@BeforeAll
590591
void beforeAll(TestInfo testInfo) {
@@ -600,6 +601,9 @@ void afterAll(TestInfo testInfo) {
600601

601602
}
602603

604+
private static class SubInstancePerClassTestCase extends InstancePerClassTestCase {
605+
}
606+
603607
@ExtendWith(InstanceTrackingExtension.class)
604608
// The following is commented out b/c it's the default.
605609
// @TestInstance(Lifecycle.PER_METHOD)

0 commit comments

Comments
 (0)