Mockito in five minutes
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
use a mock to isolate a class.
Planned topics
- A mock is a fake object you program: "when asked X, answer Y".
when(mock.method()).thenReturn(value)sets the answer;verify(mock).method()checks it was called.- Mock the collaborators, never the class under test.
- This is only the basics; JNR-044 covers mocking properly.
Draft outline details
Demonstration
var repo = mock(ProductRepository.class);
when(repo.findById(1L)).thenReturn(Optional.of(new Product(1L, "Pen")));Check your understanding
What does thenReturn do?
Show answer
Defines what the mock answers.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.