Skip to content

Commit

Permalink
Make @testinstance an @inherited annotation
Browse files Browse the repository at this point in the history
Issue: #419
  • Loading branch information
sbrannen committed Jun 30, 2017
1 parent fa8dc48 commit 76065a2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
Expand All @@ -22,13 +23,15 @@
* test class.
*
* <p>If {@code @TestInstance} is not declared on a test class, the lifecycle
* mode will default to {@link Lifecycle#PER_METHOD PER_METHOD}.
* mode will default to {@link Lifecycle#PER_METHOD PER_METHOD}. Note, however,
* that the lifecycle mode is <em>inherited</em> within test class hierarchies.
*
* @author Sam Brannen
* @since 5.0
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface TestInstance {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,12 @@
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.platform.engine.test.event.ExecutionEventRecorder;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

/**
* Integration tests for {@link TestInstance @TestInstance} lifecycle support.
*
* @since 5.0
*/
@RunWith(JUnitPlatform.class)
public

class TestInstanceLifecycleTests extends AbstractJupiterTestEngineTests {

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

@Test
void instancePerClass() {
Class<?> testClass = InstancePerClassTestCase.class;
instancePerClass(InstancePerClassTestCase.class);
}

@Test
void instancePerClassWithInheritedLifecycleMode() {
instancePerClass(SubInstancePerClassTestCase.class);
}

private void instancePerClass(Class<?> testClass) {
int containers = 3;
int tests = 3;
int instances = 1;
Expand Down Expand Up @@ -475,8 +478,6 @@ private void performAssertions(Class<?> testClass, int containers, int tests, in

ExecutionEventRecorder eventRecorder = executeTestsForClass(testClass);

// eventRecorder.eventStream().forEach(System.out::println);

// @formatter:off
assertAll(
() -> assertEquals(containers, eventRecorder.getContainerStartedCount(), "# containers started"),
Expand Down Expand Up @@ -584,7 +585,7 @@ static void afterAllStatic(TestInfo testInfo) {
}

@TestInstance(Lifecycle.PER_CLASS)
private static class InstancePerClassTestCase extends InstancePerMethodTestCase {
static class InstancePerClassTestCase extends InstancePerMethodTestCase {

@BeforeAll
void beforeAll(TestInfo testInfo) {
Expand All @@ -600,6 +601,9 @@ void afterAll(TestInfo testInfo) {

}

private static class SubInstancePerClassTestCase extends InstancePerClassTestCase {
}

@ExtendWith(InstanceTrackingExtension.class)
// The following is commented out b/c it's the default.
// @TestInstance(Lifecycle.PER_METHOD)
Expand Down

0 comments on commit 76065a2

Please sign in to comment.