Mocking substitutes controlled test doubles for components that are costly, unavailable, slow, or otherwise unsuitable for a test environment. Keep the mocked service outside the test’s primary target so the test verifies production behavior rather than the mock. section 5.3
Quarkus supports two practical mocking approaches:
- Replace a CDI bean with a test implementation annotated with @Mock, which combines @Alternative, @Priority(1), and @Dependent. Place the class under src/test/java so it is discovered for tests. section 5.3.1
- Create a mock dynamically with Mockito, define method behavior with its DSL, and install it with QuarkusMock.installMockForType. Mockito can mock any object; CDI mocks must use proxyable normal scopes. section 5.3.2
Mocking inventory availability
A ReservationResource test can replace GraphQLInventoryClient with Mockito, configure allCars() to return one Peugeot, then request availability, reserve that car, and request availability again to verify it is no longer returned. section 5.3.2
Listing 5.4 Mock implementation of the GraphQLInventoryClient
@Mock
public class MockInventoryClient implements GraphQLInventoryClient {
@Override
public List<Car> allCars() {
Car peugeot = new Car(1L, "ABC123", "Peugeot", "406");
return List.of(peugeot);
}
}
Inspect a compact CDI replacement that returns one hard-coded car from the mocked inventory client. section 5.3.1




















