From cea286fdbdc3d523029cb7b17a56af0708d75411 Mon Sep 17 00:00:00 2001 From: Jose Date: Thu, 13 Apr 2023 08:03:30 +0200 Subject: [PATCH] Avoid calling after construct callbacks twice when using @Nested tests The creation of test instances is done recursively, JUnit will call the method `initTestState` for every class. So we don't need to call the method `invokeAfterConstructCallbacks` for outer instances. Fix https://github.com/quarkusio/quarkus/issues/32383 --- .../io/quarkus/it/mockbean/WithSpiesTest.java | 17 +++++++++++++++++ .../test/junit/QuarkusTestExtension.java | 3 --- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/integration-tests/injectmock/src/test/java/io/quarkus/it/mockbean/WithSpiesTest.java b/integration-tests/injectmock/src/test/java/io/quarkus/it/mockbean/WithSpiesTest.java index 6392513c0440c1..8129e91cfd03b9 100644 --- a/integration-tests/injectmock/src/test/java/io/quarkus/it/mockbean/WithSpiesTest.java +++ b/integration-tests/injectmock/src/test/java/io/quarkus/it/mockbean/WithSpiesTest.java @@ -6,6 +6,7 @@ import jakarta.inject.Named; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -80,4 +81,20 @@ public void testOverrideDummy() { .statusCode(200) .body(is("1/2")); } + + @Nested + class WithNested { + @Test + @DisplayName("Verify default Greeting values are returned from Spied objects") + public void testGreet() { + given() + .when().get("/greeting") + .then() + .statusCode(200) + .body(is("HELLO")); + Mockito.verify(capitalizerService, Mockito.times(1)).capitalize(Mockito.eq("hello")); + Mockito.verify(messageService, Mockito.times(1)).getMessage(); + Mockito.verify(suffixService, Mockito.times(1)).getSuffix(); + } + } } diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java index a0b2ac6fc2c8b9..73c7260696caf3 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java @@ -765,9 +765,6 @@ private void initTestState(ExtensionContext extensionContext, QuarkusTestExtensi } invokeAfterConstructCallbacks(Object.class, actualTestInstance); - for (Object outerInstance : outerInstances) { - invokeAfterConstructCallbacks(Object.class, outerInstance); - } } catch (Exception e) { throw new TestInstantiationException("Failed to create test instance", e instanceof InvocationTargetException ? e.getCause() : e);