Skip to content

Commit 4e046f5

Browse files
authored
Merge pull request #26644 from Sgitario/fix_tests
Clear outer instances when init test state is called
2 parents e8054c9 + 28fc5cf commit 4e046f5

9 files changed

+83
-8
lines changed

integration-tests/main/src/test/java/io/quarkus/it/main/QuarkusTestCallbacksTestCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import io.quarkus.test.junit.QuarkusTest;
2424

2525
/**
26-
* The purpose of this test is simply to ensure that {@link SimpleAnnotationCheckerBeforeEachCallback}
26+
* The purpose of this test is simply to ensure that {@link TestContextCheckerBeforeEachCallback}
2727
* can read {@code @TestAnnotation} without issue.
2828
* Also checks that {@link SimpleAnnotationCheckerBeforeClassCallback} is executed properly
2929
*/
@@ -69,7 +69,7 @@ private void checkBeforeOrAfterEachTestInfo(TestInfo testInfo, String unexpected
6969
@TestAnnotation
7070
@Order(1)
7171
public void testTestMethodHasAnnotation() {
72-
assertTrue(SimpleAnnotationCheckerBeforeEachCallback.testAnnotationChecked);
72+
assertTrue(TestContextCheckerBeforeEachCallback.testAnnotationChecked);
7373
}
7474

7575
@Test

integration-tests/main/src/test/java/io/quarkus/it/main/QuarkusTestNestedTestCase.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ void test() {
5959
assertEquals(0, COUNT_TEST.getAndIncrement(), "COUNT_TEST");
6060
assertEquals(0, COUNT_AFTER_EACH.get(), "COUNT_AFTER_EACH");
6161
assertEquals(0, COUNT_AFTER_ALL.get(), "COUNT_AFTER_ALL");
62+
assertEquals(0, TestContextCheckerBeforeEachCallback.OUTER_INSTANCES.size(), "Found unexpected outer instances");
6263
}
6364

6465
@Nested
@@ -93,6 +94,18 @@ void testTwo() {
9394
assertEquals(0, COUNT_AFTER_ALL.get(), "COUNT_AFTER_ALL");
9495
}
9596

97+
@Test
98+
@Order(3)
99+
void testOuterInstancesInBeforeEach() {
100+
assertEquals(1, TestContextCheckerBeforeEachCallback.OUTER_INSTANCES.size());
101+
}
102+
103+
@Test
104+
@Order(4)
105+
void testOuterInstancesInAfterEach() {
106+
assertEquals(1, TestContextCheckerAfterEachCallback.OUTER_INSTANCES.size());
107+
}
108+
96109
@Test
97110
void testInnerAndOuterValues() {
98111
assertEquals(EXPECTED_INNER_VALUE, innerValue);
@@ -122,7 +135,6 @@ void beforeEach() {
122135
@Test
123136
@Order(1)
124137
void testOne() {
125-
// assertEquals(1, SECOND_LEVEL_COUNTER.get(), "SECOND_LEVEL_COUNTER");
126138
assertEquals(1, SECOND_LEVEL_COUNTER.get(), "SECOND_LEVEL_COUNTER");
127139
}
128140

@@ -134,6 +146,24 @@ void testSecondLevelAndInnerAndOuterValues() {
134146
assertEquals(EXPECTED_OUTER_VALUE, outerValue);
135147
assertEquals(EXPECTED_SECOND_LEVEL_FIRST_INNER_VALUE, secondLevelInnerValue);
136148
}
149+
150+
@Test
151+
@Order(3)
152+
void testOuterInstancesInBeforeEach() {
153+
assertEquals(2, TestContextCheckerBeforeEachCallback.OUTER_INSTANCES.size());
154+
}
155+
156+
@Test
157+
@Order(4)
158+
void testOuterInstancesInAfterEach() {
159+
assertEquals(2, TestContextCheckerAfterEachCallback.OUTER_INSTANCES.size());
160+
}
161+
162+
@Test
163+
@Order(5)
164+
void testOuterInstancesInAfterAll() {
165+
assertEquals(1, TestContextCheckerAfterAllCallback.OUTER_INSTANCES.size());
166+
}
137167
}
138168
}
139169

@@ -185,9 +215,9 @@ void afterEach() {
185215
@AfterAll
186216
static void afterAll() {
187217
assertEquals(1, COUNT_BEFORE_ALL.get(), "COUNT_BEFORE_ALL");
188-
assertEquals(15, COUNT_BEFORE_EACH.get(), "COUNT_BEFORE_EACH");
218+
assertEquals(25, COUNT_BEFORE_EACH.get(), "COUNT_BEFORE_EACH");
189219
assertEquals(4, COUNT_TEST.get(), "COUNT_TEST");
190-
assertEquals(15, COUNT_AFTER_EACH.get(), "COUNT_AFTER_EACH");
220+
assertEquals(25, COUNT_AFTER_EACH.get(), "COUNT_AFTER_EACH");
191221
assertEquals(0, COUNT_AFTER_ALL.getAndIncrement(), "COUNT_AFTER_ALL");
192222
}
193223
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.quarkus.it.main;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import io.quarkus.test.junit.callback.QuarkusTestAfterAllCallback;
7+
import io.quarkus.test.junit.callback.QuarkusTestContext;
8+
9+
public class TestContextCheckerAfterAllCallback implements QuarkusTestAfterAllCallback {
10+
11+
public static final List<Object> OUTER_INSTANCES = new ArrayList<>();
12+
13+
@Override
14+
public void afterAll(QuarkusTestContext context) {
15+
OUTER_INSTANCES.clear();
16+
OUTER_INSTANCES.addAll(context.getOuterInstances());
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.quarkus.it.main;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback;
7+
import io.quarkus.test.junit.callback.QuarkusTestMethodContext;
8+
9+
public class TestContextCheckerAfterEachCallback implements QuarkusTestAfterEachCallback {
10+
11+
public static final List<Object> OUTER_INSTANCES = new ArrayList<>();
12+
13+
@Override
14+
public void afterEach(QuarkusTestMethodContext context) {
15+
OUTER_INSTANCES.clear();
16+
OUTER_INSTANCES.addAll(context.getOuterInstances());
17+
}
18+
}
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
package io.quarkus.it.main;
22

33
import java.lang.reflect.Method;
4+
import java.util.ArrayList;
5+
import java.util.List;
46

57
import io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback;
68
import io.quarkus.test.junit.callback.QuarkusTestMethodContext;
79

8-
public class SimpleAnnotationCheckerBeforeEachCallback implements QuarkusTestBeforeEachCallback {
10+
public class TestContextCheckerBeforeEachCallback implements QuarkusTestBeforeEachCallback {
911

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

1215
@Override
1316
public void beforeEach(QuarkusTestMethodContext context) {
14-
// make sure that this comes into play only for the test we care about
17+
OUTER_INSTANCES.clear();
18+
OUTER_INSTANCES.addAll(context.getOuterInstances());
19+
20+
// continue only if this comes into play only for the test we care about
1521

1622
Method testMethod = context.getTestMethod();
1723
if (!testMethod.getDeclaringClass().getName().endsWith("QuarkusTestCallbacksTestCase")) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
io.quarkus.it.main.TestContextCheckerAfterAllCallback
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
io.quarkus.it.main.TestContextCheckerAfterEachCallback
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
io.quarkus.it.main.SimpleAnnotationCheckerBeforeEachCallback
1+
io.quarkus.it.main.TestContextCheckerBeforeEachCallback

test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ private void initTestState(ExtensionContext extensionContext, ExtensionState sta
776776
outerInstances.add(outerInstance);
777777
}
778778
} else {
779+
outerInstances.clear();
779780
actualTestInstance = runningQuarkusApplication.instance(actualTestClass);
780781
}
781782

0 commit comments

Comments
 (0)