Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.quarkus.it.mockbean;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import jakarta.enterprise.context.ApplicationScoped;

import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectSpy;

@QuarkusTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class PerClassSpyTest {

@InjectSpy
IdentityService identityService;

@Test
@Order(1)
void testWithSpy() {
when(identityService.call(any())).thenReturn("DUMMY");
assertEquals("DUMMY", identityService.call("foo"));
}

@Test
@Order(2)
void testWithoutSpy() {
assertEquals("foo", identityService.call("foo"));
}

@ApplicationScoped
public static class IdentityService {

public String call(String input) {
return input;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ static Set<Mocked> getMocks(Object testInstance) {
}

static void reset(Object testInstance) {
Mockito.framework().clearInlineMock(testInstance);
for (Mocked m : getMocks(testInstance)) {
Mockito.framework().clearInlineMock(m.mock);
}
}

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

static class Mocked {
final Object mock;
final Object beanInstance;

public Mocked(Object mock, Object beanInstance) {
this.mock = mock;
this.beanInstance = beanInstance;
}
record Mocked(Object mock, Object beanInstance) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public void beforeEach(QuarkusTestMethodContext context) {

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