Skip to content
Ameen Abozaid
Planned lessonJNR-016-L03

Mockito in five minutes

  • Lesson 3 of 7
  • Junior
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

java
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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.