From 51d851722329f526398d073c50236992a785f7ea 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 (cherry picked from commit 72b1fa13984e7db902567d5dadeb1cf8511434d2) --- .../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 6392513c0440c..8129e91cfd03b 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 a0b2ac6fc2c8b..73c7260696caf 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);