liveBook · Manning

Mock method

As explained in Spring Start Here, Second Edition

The Mockito mock() method creates a mock object for a specified Java type.

Mockito’s mock() method creates a mock object for a Java type. In a unit test, it can replace a real dependency so the test can focus on the object’s logic. section 15.2

Mocking a repository

Create the mock with `accountRepository = mock(AccountRepository.class);` and pass it to `TransferService`. Configure `findById()` with `given()` to return test accounts, call `transferMoney()`, then use `verify()` to check the repository calls. section 15.2

How Spring Start Here, Second Edition covers it

How deep it goes
Explains mock creation and demonstrates configuring behavior and verifying calls, without detailing Mockito internals.
Examples
Uses an AccountRepository mock in a money-transfer test, stubbing account lookups and verifying updated amounts.
What you can do after
Build a unit test that controls a dependency with Mockito and verifies how the tested method uses it.
How it connects
Connects Mockito mocks with unit testing, dependency isolation, and JUnit.

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.