Java Mockito Verify Tutorial 🎯

beginner
11 min

Java Mockito Verify Tutorial 🎯

Welcome to our comprehensive guide on using Mockito for verification in Java! In this tutorial, we'll explore how to verify the behavior of your mocks to ensure they're working as expected. Let's dive in! πŸ’‘

What is Mockito Verify?

Mockito Verify is a method used to check whether a specific method call was made on a mock object during the execution of your test. This is crucial for ensuring that your mocks are behaving as intended and interacting with other objects in the correct manner. πŸ“

Setting Up the Environment πŸ”§

Before we get started, make sure you have the following:

  • Java Development Kit (JDK) installed
  • Mockito library added to your project (usually through Maven or Gradle)

Mockito Dependency for Maven

Add this to your pom.xml:

xml
<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>4.0.0</version> <scope>test</scope> </dependency>

Mockito Dependency for Gradle

Add this to your build.gradle:

groovy
testImplementation 'org.mockito:mockito-core:4.0.0'

Creating a Mock Object πŸ¦Έβ€β™‚οΈ

Let's start by creating a mock object for a service:

java
import static org.mockito.Mockito.*; public class ExampleTest { private ExampleService exampleService; @Before public void setup() { exampleService = mock(ExampleService.class); } }

In this example, we're creating a mock object for the ExampleService class.

Verifying Method Calls πŸ•΅οΈβ€β™‚οΈ

Now that we have our mock object, let's verify that certain methods were called:

java
// Given when(exampleService.doSomething("arg1", "arg2")).thenReturn("result"); // When String result = exampleService.doSomething("arg1", "arg2"); // Then verify(exampleService).doSomething("arg1", "arg2"); βœ…

In the above example, we first set up a mock response using when(). Then, we call the method on our mock object. Finally, we use verify() to check that the method call occurred as expected.

Verifying Method Calls with Arguments πŸ•΅οΈβ€β™€οΈ

Sometimes, you'll need to verify that specific arguments were passed to a method:

java
// Given when(exampleService.doSomething("arg1", "arg2")).thenReturn("result"); // When String result = exampleService.doSomething("arg1", "arg2"); // Then verify(exampleService).doSomething("arg1", eq("arg2")); βœ…

In this example, we're verifying that the second argument passed to the doSomething() method was "arg2".

Verifying Method Calls Multiple Times πŸ”„

If you need to verify that a method call occurred a specific number of times:

java
// Given when(exampleService.doSomething("arg1", "arg2")).thenReturn("result"); // When exampleService.doSomething("arg1", "arg2"); exampleService.doSomething("arg1", "arg2"); // Then verify(exampleService, times(2)).doSomething("arg1", "arg2"); βœ…

In this example, we're verifying that the doSomething() method was called twice with the specified arguments.

Quick Quiz
Question 1 of 1

Which Mockito method is used to check whether a specific method call was made on a mock object during the execution of your test?

Verifying Method Call Order πŸ•°οΈ

Mockito also allows you to verify the order in which methods were called:

java
// Given when(exampleService.doFirst("arg1")).thenReturn("result1"); when(exampleService.doSecond("arg1")).thenReturn("result2"); // When exampleService.doFirst("arg1"); exampleService.doSecond("arg1"); // Then verify(exampleService).doFirst("arg1"); verify(exampleService).doSecond("arg1"); verify(exampleService).doFirst("arg1").before(exampleService).doSecond("arg1"); βœ…

In this example, we're verifying that the doFirst() method was called before the doSecond() method with the specified arguments.

Quick Quiz
Question 1 of 1

Which Mockito method is used to verify the order in which methods were called?

That's it for our Mockito Verify tutorial! Remember to always write clean, testable code, and use mocking to isolate and test your components effectively. Happy coding! πŸ€–πŸ’»