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
  • Loading branch information
Sgitario authored and holly-cummins committed Apr 20, 2023
1 parent 6acce5d commit b45763e
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 b45763e

Please sign in to comment.