liveBook · Manning

Stub

As explained in Testing Spring Boot Applications

A stub is a test substitute that provides predetermined or programmable behavior in place of a real dependency.

A stub replaces a dependency with an implementation that returns a pre-programmed response. In a Spring Boot test, it lets the application under test receive predictable values without relying on the dependency’s real behavior. section 3.4

A deliberately limited test context may fail to start because a selected configuration creates beans whose dependencies were not included. Supplying test substitutes satisfies those dependencies without pulling in the real classes and their own dependency graphs. A custom @TestConfiguration can provide stub implementations when a small, manually selected test application is useful. section 3.3.4 section 3.4

Listing 3.13 Providing beans with @MockitoBean
@SpringBootTest(classes = { ThingConfiguration.class }) #1
class CustomClassesTests {

  @MockitoBean #2
  Gizmo gizmo;

  @MockitoBean(name = "blueWidget") #3
  Widget blueWidget;

  // ... your test code ...


}

@MockitoBean supplies the missing Gizmo and Widget beans for a test that loads only ThingConfiguration. The Widget substitute is named because bean-name-based autowiring must distinguish it from another Widget. These mocks initially have default behavior; program a response when the test needs a stub rather than merely a bean that satisfies wiring. section 3.4

The distinction is whether the test uses the substitute’s behavior. An unused mock that only allows the context to start is a dummy; a mock programmed to return a specific value is a stub. @TestBean is another way to provide test beans through static factory methods. Use the lightest substitute that supports the behavior being tested. section 3.4 section 3.3.4

Stubs also keep Spring Boot tests independent of many external services and can simulate dependency failures such as timeouts, protocol errors, or invalid data. They support repeatable application tests, but do not make every test a good fit for Spring Boot: narrow code with many conditional branches may be better tested at a smaller scope. section 1.2

How Testing Spring Boot Applications covers it

How deep it goes
Distinguishes stubs from unused dummy mocks and shows bean-supply options; stops short of demonstrating a programmed Mockito response.
Examples
Shows missing Gizmo and named Widget beans supplied with @MockitoBean, alongside an @TestBean factory-method alternative.
What it assumes
Assumes familiarity with Spring Boot tests, bean configuration, and autowiring while defining the distinction between a dummy and a stub.
What you can do after
Apply it via @MockitoBean or @TestBean to supply test dependencies, programming responses when their behavior matters.
How it connects
Connects stubs to Mockito mocks, Spring beans, dependency injection, @TestConfiguration, and @TestBean.

Also explained in

Compare how 14 books explain stub →

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.