liveBook · Manning

Mocking

As explained in Spring Start Here, Second Edition

A mock object is a controllable test double that simulates a dependency and allows its interactions to be inspected or verified.

A unit test should focus on one piece of logic, but that logic may depend on other components. Mocking replaces a dependency with a controllable fake object, so the test can choose how the dependency behaves and check how the tested code uses it. section 15.2 section 15.4

A test using a mock has three main steps:

  • Set up the inputs and mock behavior. For example, tell a repository mock which account to return for an ID. section 15.2 section 15.4
  • Call the method under test with the chosen inputs. section 15.4
  • Check the outcome, including any expected calls to the mock or calls that must not happen. section 15.2

Testing a money transfer

Create an AccountRepository mock and give it to TransferService. Configure findById() to return a sender and receiver with 1,000 each. After transferring 100, verify that changeAmount() was called with 900 for the sender and 1,100 for the receiver. The test checks the service’s calculations without using the real repository. section 15.2

Listing 15.4 A unit test validating the happy flow
public class TransferServiceUnitTests {
 
  // Omitted code
 
 @Test
    public void transferMoneyHappyFlow() {
    Account sender = new Account();
    sender.setId(1L);
    sender.setAmount(new BigDecimal("1000"));
 
    Account receiver = new Account();
    receiver.setId(2L);
    receiver.setAmount(new BigDecimal("1000"));
 
    given(accountRepository.findById(1L))
      .willReturn(Optional.of(sender));
 
    given(accountRepository.findById(2L))
      .willReturn(Optional.of(receiver));
 
    transferService.transferMoney(1L, 2L, new BigDecimal("100")); 
 
    verify(accountRepository)     #A
      .changeAmount(1L, new BigDecimal("900"));    #A
    verify(accountRepository)     #A
      .changeAmount(2L, new BigDecimal("1100"));     #A
  }
}

See the complete happy-flow test, from configured repository responses to verified amount changes. section 15.2

Testing a missing account

Configure the receiver lookup to return an empty result. The test checks that transferMoney() throws AccountNotFoundException and uses verify(..., never()) to confirm that no amount is changed. section 15.2

Mockito can create a mock explicitly with mock(). Alternatively, MockitoExtension with @Mock and @InjectMocks provides an annotation-based setup. Mocking isolates the chosen logic; an integration test instead checks how components work together. section 15.2 section 15.4

How Spring Start Here, Second Edition covers it

How deep it goes
Walks through mock creation, configured responses, method execution, and verification, including an exception flow.
Examples
Uses money transfers and login controller tests to demonstrate successful and failed flows.
What you can do after
Apply it via Mockito to configure dependency responses and verify expected or forbidden calls.
How it connects
Connects mocking to unit testing, integration testing, dependency isolation, and interaction verification.

Also explained in

Compare how 26 books explain mocking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.