liveBook · Manning

Mocking

As explained in Quarkus in Action

Mocking is the practice of substituting controlled test doubles for components that are unsuitable or undesirable to run during a test.

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

How Quarkus in Action covers it

How deep it goes
Explains CDI replacement and Mockito behavior, including scope constraints and native-mode limitations, but stops short of exhaustive framework reference.
Examples
Demonstrates a mocked GraphQLInventoryClient returning one Peugeot during a ReservationResource availability-and-reservation test.
What it assumes
Assumes familiarity with Quarkus tests and CDI injection while teaching the two mocking approaches from their practical purpose.
What you can do after
Apply it via Quarkus CDI alternatives or Mockito to test application behavior without running the real remote service.
How it connects
Connects CDI beans, alternatives, Mockito, QuarkusMock, JUnit 5, REST testing, and native mode.

Also explained in

Compare how 21 books explain mocking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Quarkus in Action. The section links point to the parts of the book it draws on.