liveBook · Manning

Mockito

As explained in Quarkus in Action

Mockito is a Java mocking framework for creating test doubles and specifying their behavior dynamically.

Mockito is a Java mocking framework for replacing collaborators with dynamically configured test doubles. It keeps tests focused on production behavior when remote services are costly, unavailable, slow, or unsuitable for the test environment. section 5.3 section 5.3.2

A Quarkus test commonly uses Mockito in this sequence:

  • Add the test-scoped io.quarkus:quarkus-junit5-mockito Maven artifact. section 5.3.2
  • Create a mock for an object, configure method results with Mockito.when(...).thenReturn(...), and install it for the target CDI type with QuarkusMock. section 5.3.2
  • Exercise the application through its resource and verify behavior without calling the real inventory service. section 5.3

Unlike replacing a CDI implementation with a test class, Mockito can mock any object. CDI use requires a normal, proxyable scope; @Singleton and @Dependent beans cannot be mocked, while other built-in CDI scopes work. Mock-based tests also cannot run in native mode when they use mocks or inject CDI into the test itself, because the application runs in another process. section 5.3.2 section 5.6

Mocking inventory availability

Create a GraphQLInventoryClient mock, configure allCars() to return one Peugeot, and install it for that interface. Call the availability endpoint, reserve the returned car, then call availability again to check that the reserved car is no longer offered.

How Quarkus in Action covers it

How deep it goes
Explains dynamic mock creation, configured behavior, CDI installation, scope limits, and native-mode constraints.
Examples
Uses a mocked GraphQLInventoryClient that returns one Peugeot during an availability-and-reservation test.
What it assumes
Assumes Java, Quarkus testing, CDI, and the collaborator interface; teaches mock configuration and installation.
What you can do after
Apply it via QuarkusMock and Mockito DSL to isolate collaborators while testing application behavior.
How it connects
Connects test doubles with Quarkus testing, CDI beans, GraphQL clients, REST resources, and native mode.

Also explained in

Compare how 2 books explain Mockito →

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.