liveBook · Manning

Mocking

As explained in Testing Spring Boot Applications

Mocking is a testing technique that replaces collaborators with configurable objects that imitate their APIs and behavior.

Mocking replaces a collaborator with an object that has the same API but programmable behavior. In a Spring Boot test, this lets the application run without calling a database, remote service, or other dependency outside the test’s scope. Mockito supplies the mock objects; Spring testing support can place them in the application context. section 1.1 section 2.6

Use @MockitoBean on a test field to replace a Spring bean with a Mockito mock. If a narrowly configured test context lacks a required bean, the annotation can supply one so the context starts. Specify a bean name when the type alone does not identify the intended bean. section 3.4 section 1.4

Listing 2.8 Replace a bean in the application context by a mock.
@MockitoBean
private WeatherService weatherService; #1

@Test
void getWeather() {
  when(weatherService.getCurrentWeather(anyDouble(), anyDouble())) #2
    .thenReturn(new WeatherData(22.6, 0, 1)); #2
  selectCity("Paris");

  var response = mvc.get().uri("/api/weather").exchange();

  // ... assertions omitted for brevity ...
}

The weather API test replaces WeatherService and programs getCurrentWeather(...) to return WeatherData with a temperature of 22.6. The HTTP response can then be checked against a predictable result without contacting the weather provider. section 2.6

A mock does not need programmed behavior when it merely fills a dependency slot: that use is a dummy. When a test programs a return value, the mock acts as a stub. Mockito can also verify an interaction, such as whether a method was called with particular arguments. By default, unprogrammed Mockito mocks provide default values, often null; do not mistake those values for meaningful application behavior. section 3.4 section 1.1

Keep the replacement at the boundary you intend to control. A live weather API can make an otherwise valid test fail because of outages, changing data, latency, or network instability. Mocking makes those tests more predictable and can simulate dependency errors, but it does not test the real external service. Calling that service belongs, when needed, in broader end-to-end testing. section 2.6 section 1.2

A Mockito mock is not the only test-specific bean. Use @TestBean to provide an explicitly constructed implementation when that suits the test better. And do not confuse Mockito mocks with MockMvc: MockMvc sends test HTTP requests to the application, while @MockitoBean replaces a collaborator inside its Spring context. section 3.4 section 1.4

How Testing Spring Boot Applications covers it

How deep it goes
Explains bean replacement, programmed responses, dummy defaults, and verification; does not develop Mockito internals.
Examples
Uses a mocked WeatherService returning 22.6 and named mock beans that satisfy a restricted test context.
What it assumes
Assumes familiarity with Java tests and Spring beans while defining what mocks, stubs, and dummy objects do.
What you can do after
Apply it via Mockito and @MockitoBean to replace collaborators, program responses, and verify interactions.
How it connects
Connects Mockito to Spring beans, stubs, integration tests, external services, @TestBean, and MockMvc.

Also explained in

Compare how 26 books explain mocking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Testing Spring Boot Applications. The section links point to the parts of the book it draws on.