Skip to content

Commit

Permalink
Avoid calling after construct callbacks twice when using @nested tests
Browse files Browse the repository at this point in the history
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 quarkusio#32383

(cherry picked from commit 72b1fa1)
  • Loading branch information
Sgitario authored and gsmet committed Apr 18, 2023
1 parent c2a9d7a commit 51d8517
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 51d8517

Please sign in to comment.