Skip to content

Commit

Permalink
Clear outer instances when init test state is called
Browse files Browse the repository at this point in the history
The init test state is called several times depending whether JUnit 5 is configured with the lifecycle method based or class based. 

Clearing out the outer instances every time the main class is initialized, ensure that the outer instances are always up to date.
  • Loading branch information
Sgitario committed Jul 11, 2022
1 parent 1d5e7bc commit 28fc5cf
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 8 deletions.
Expand Up @@ -23,7 +23,7 @@
import io.quarkus.test.junit.QuarkusTest;

/**
* The purpose of this test is simply to ensure that {@link SimpleAnnotationCheckerBeforeEachCallback}
* The purpose of this test is simply to ensure that {@link TestContextCheckerBeforeEachCallback}
* can read {@code @TestAnnotation} without issue.
* Also checks that {@link SimpleAnnotationCheckerBeforeClassCallback} is executed properly
*/
Expand Down Expand Up @@ -69,7 +69,7 @@ private void checkBeforeOrAfterEachTestInfo(TestInfo testInfo, String unexpected
@TestAnnotation
@Order(1)
public void testTestMethodHasAnnotation() {
assertTrue(SimpleAnnotationCheckerBeforeEachCallback.testAnnotationChecked);
assertTrue(TestContextCheckerBeforeEachCallback.testAnnotationChecked);
}

@Test
Expand Down
Expand Up @@ -59,6 +59,7 @@ void test() {
assertEquals(0, COUNT_TEST.getAndIncrement(), "COUNT_TEST");
assertEquals(0, COUNT_AFTER_EACH.get(), "COUNT_AFTER_EACH");
assertEquals(0, COUNT_AFTER_ALL.get(), "COUNT_AFTER_ALL");
assertEquals(0, TestContextCheckerBeforeEachCallback.OUTER_INSTANCES.size(), "Found unexpected outer instances");
}

@Nested
Expand Down Expand Up @@ -93,6 +94,18 @@ void testTwo() {
assertEquals(0, COUNT_AFTER_ALL.get(), "COUNT_AFTER_ALL");
}

@Test
@Order(3)
void testOuterInstancesInBeforeEach() {
assertEquals(1, TestContextCheckerBeforeEachCallback.OUTER_INSTANCES.size());
}

@Test
@Order(4)
void testOuterInstancesInAfterEach() {
assertEquals(1, TestContextCheckerAfterEachCallback.OUTER_INSTANCES.size());
}

@Test
void testInnerAndOuterValues() {
assertEquals(EXPECTED_INNER_VALUE, innerValue);
Expand Down Expand Up @@ -122,7 +135,6 @@ void beforeEach() {
@Test
@Order(1)
void testOne() {
// assertEquals(1, SECOND_LEVEL_COUNTER.get(), "SECOND_LEVEL_COUNTER");
assertEquals(1, SECOND_LEVEL_COUNTER.get(), "SECOND_LEVEL_COUNTER");
}

Expand All @@ -134,6 +146,24 @@ void testSecondLevelAndInnerAndOuterValues() {
assertEquals(EXPECTED_OUTER_VALUE, outerValue);
assertEquals(EXPECTED_SECOND_LEVEL_FIRST_INNER_VALUE, secondLevelInnerValue);
}

@Test
@Order(3)
void testOuterInstancesInBeforeEach() {
assertEquals(2, TestContextCheckerBeforeEachCallback.OUTER_INSTANCES.size());
}

@Test
@Order(4)
void testOuterInstancesInAfterEach() {
assertEquals(2, TestContextCheckerAfterEachCallback.OUTER_INSTANCES.size());
}

@Test
@Order(5)
void testOuterInstancesInAfterAll() {
assertEquals(1, TestContextCheckerAfterAllCallback.OUTER_INSTANCES.size());
}
}
}

Expand Down Expand Up @@ -185,9 +215,9 @@ void afterEach() {
@AfterAll
static void afterAll() {
assertEquals(1, COUNT_BEFORE_ALL.get(), "COUNT_BEFORE_ALL");
assertEquals(15, COUNT_BEFORE_EACH.get(), "COUNT_BEFORE_EACH");
assertEquals(25, COUNT_BEFORE_EACH.get(), "COUNT_BEFORE_EACH");
assertEquals(4, COUNT_TEST.get(), "COUNT_TEST");
assertEquals(15, COUNT_AFTER_EACH.get(), "COUNT_AFTER_EACH");
assertEquals(25, COUNT_AFTER_EACH.get(), "COUNT_AFTER_EACH");
assertEquals(0, COUNT_AFTER_ALL.getAndIncrement(), "COUNT_AFTER_ALL");
}
}
@@ -0,0 +1,18 @@
package io.quarkus.it.main;

import java.util.ArrayList;
import java.util.List;

import io.quarkus.test.junit.callback.QuarkusTestAfterAllCallback;
import io.quarkus.test.junit.callback.QuarkusTestContext;

public class TestContextCheckerAfterAllCallback implements QuarkusTestAfterAllCallback {

public static final List<Object> OUTER_INSTANCES = new ArrayList<>();

@Override
public void afterAll(QuarkusTestContext context) {
OUTER_INSTANCES.clear();
OUTER_INSTANCES.addAll(context.getOuterInstances());
}
}
@@ -0,0 +1,18 @@
package io.quarkus.it.main;

import java.util.ArrayList;
import java.util.List;

import io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback;
import io.quarkus.test.junit.callback.QuarkusTestMethodContext;

public class TestContextCheckerAfterEachCallback implements QuarkusTestAfterEachCallback {

public static final List<Object> OUTER_INSTANCES = new ArrayList<>();

@Override
public void afterEach(QuarkusTestMethodContext context) {
OUTER_INSTANCES.clear();
OUTER_INSTANCES.addAll(context.getOuterInstances());
}
}
@@ -1,17 +1,23 @@
package io.quarkus.it.main;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback;
import io.quarkus.test.junit.callback.QuarkusTestMethodContext;

public class SimpleAnnotationCheckerBeforeEachCallback implements QuarkusTestBeforeEachCallback {
public class TestContextCheckerBeforeEachCallback implements QuarkusTestBeforeEachCallback {

public static final List<Object> OUTER_INSTANCES = new ArrayList<>();
static boolean testAnnotationChecked;

@Override
public void beforeEach(QuarkusTestMethodContext context) {
// make sure that this comes into play only for the test we care about
OUTER_INSTANCES.clear();
OUTER_INSTANCES.addAll(context.getOuterInstances());

// continue only if this comes into play only for the test we care about

Method testMethod = context.getTestMethod();
if (!testMethod.getDeclaringClass().getName().endsWith("QuarkusTestCallbacksTestCase")) {
Expand Down
@@ -0,0 +1 @@
io.quarkus.it.main.TestContextCheckerAfterAllCallback
@@ -0,0 +1 @@
io.quarkus.it.main.TestContextCheckerAfterEachCallback
@@ -1 +1 @@
io.quarkus.it.main.SimpleAnnotationCheckerBeforeEachCallback
io.quarkus.it.main.TestContextCheckerBeforeEachCallback
Expand Up @@ -776,6 +776,7 @@ private void initTestState(ExtensionContext extensionContext, ExtensionState sta
outerInstances.add(outerInstance);
}
} else {
outerInstances.clear();
actualTestInstance = runningQuarkusApplication.instance(actualTestClass);
}

Expand Down

0 comments on commit 28fc5cf

Please sign in to comment.