Skip to content

Commit 83f8ebe

Browse files
authored
Merge pull request #47181 from geoand/#47179
Properly reset mocks
2 parents 397237e + dd998af commit 83f8ebe

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.quarkus.it.mockbean;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.mockito.ArgumentMatchers.any;
5+
import static org.mockito.Mockito.when;
6+
7+
import jakarta.enterprise.context.ApplicationScoped;
8+
9+
import org.junit.jupiter.api.MethodOrderer;
10+
import org.junit.jupiter.api.Order;
11+
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.api.TestInstance;
13+
import org.junit.jupiter.api.TestMethodOrder;
14+
15+
import io.quarkus.test.junit.QuarkusTest;
16+
import io.quarkus.test.junit.mockito.InjectSpy;
17+
18+
@QuarkusTest
19+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
20+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
21+
public class PerClassSpyTest {
22+
23+
@InjectSpy
24+
IdentityService identityService;
25+
26+
@Test
27+
@Order(1)
28+
void testWithSpy() {
29+
when(identityService.call(any())).thenReturn("DUMMY");
30+
assertEquals("DUMMY", identityService.call("foo"));
31+
}
32+
33+
@Test
34+
@Order(2)
35+
void testWithoutSpy() {
36+
assertEquals("foo", identityService.call("foo"));
37+
}
38+
39+
@ApplicationScoped
40+
public static class IdentityService {
41+
42+
public String call(String input) {
43+
return input;
44+
}
45+
}
46+
}

test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/internal/MockitoMocksTracker.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ static Set<Mocked> getMocks(Object testInstance) {
2525
}
2626

2727
static void reset(Object testInstance) {
28-
Mockito.framework().clearInlineMock(testInstance);
28+
for (Mocked m : getMocks(testInstance)) {
29+
Mockito.framework().clearInlineMock(m.mock);
30+
}
2931
}
3032

3133
static Optional<Object> currentMock(Object testInstance, Object beanInstance) {
@@ -38,13 +40,6 @@ static Optional<Object> currentMock(Object testInstance, Object beanInstance) {
3840
return Optional.empty();
3941
}
4042

41-
static class Mocked {
42-
final Object mock;
43-
final Object beanInstance;
44-
45-
public Mocked(Object mock, Object beanInstance) {
46-
this.mock = mock;
47-
this.beanInstance = beanInstance;
48-
}
43+
record Mocked(Object mock, Object beanInstance) {
4944
}
5045
}

test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/internal/SetMockitoMockAsBeanMockCallback.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public void beforeEach(QuarkusTestMethodContext context) {
1818

1919
private void installMock(MockitoMocksTracker.Mocked mocked) {
2020
try {
21-
QuarkusMock.installMockForInstance(mocked.mock, mocked.beanInstance);
21+
QuarkusMock.installMockForInstance(mocked.mock(), mocked.beanInstance());
2222
} catch (Exception e) {
23-
throw new RuntimeException(mocked.beanInstance
23+
throw new RuntimeException(mocked.beanInstance()
2424
+ " is not a normal scoped CDI bean, make sure the bean is a normal scope like @ApplicationScoped or @RequestScoped."
2525
+ " Alternatively you can use '@InjectMock(convertScopes=true)' instead of '@InjectMock' if you would like"
2626
+ " Quarkus to automatically make that conversion (you should only use this if you understand the implications).");

0 commit comments

Comments
 (0)