Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear outer instances when init test state is called #26644

Merged
merged 1 commit into from
Jul 12, 2022
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 @@ -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
Original file line number Diff line number Diff line change
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");
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.quarkus.it.main.TestContextCheckerAfterAllCallback
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.quarkus.it.main.TestContextCheckerAfterEachCallback
Original file line number Diff line number Diff line change
@@ -1 +1 @@
io.quarkus.it.main.SimpleAnnotationCheckerBeforeEachCallback
io.quarkus.it.main.TestContextCheckerBeforeEachCallback
Original file line number Diff line number Diff line change
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