Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I verify the behavior of a mock object in JUnit 5 using Mockito


How do I verify the behavior of a mock object in JUnit 5 using Mockito


Verifying the behavior of a mock object in JUnit 5 using Mockito involves several steps and techniques. Here's a detailed guide on how to do it effectively:

1. Setting Up Mockito with JUnit 5

First, ensure that you have Mockito and JUnit 5 in your project. You can include them in your `pom.xml` if you're using Maven or in your `build.gradle` if you're using Gradle.

To use Mockito with JUnit 5, you need to extend your test class with `@ExtendWith(MockitoExtension.class)`. This extension automatically initializes and injects mock objects annotated with `@Mock`.

java
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class MockitoVerifyTest {

    @Mock
    private Database database;

    // Your tests here
}

2. Creating and Configuring Mocks

You can create mock objects using the `@Mock` annotation or manually with `mock()` method from Mockito. Once created, you can configure their behavior using `when()` method.

java
when(database.getUniqueId()).thenReturn(43);

3. Using the Mock Object in Your Test

After setting up the mock, use it in your test code. For example, you might call methods on it to simulate interactions.

java
database.setUniqueId(12);
database.getUniqueId();
database.getUniqueId();

4. Verifying Method Calls

The `verify()` method is used to check if the expected method calls were made on the mock object. You can verify the number of times a method was called and the arguments passed to it.

Basic Verification

By default, `verify()` checks if a method was called once.

java
verify(database).setUniqueId(12);
verify(database).getUniqueId();

Verification Modes

You can specify how many times a method should have been called using various verification modes:

- `times()`: Verifies that a method was called a specific number of times.
- `atLeast()`: Verifies that a method was called at least a certain number of times.
- `atLeastOnce()`: Verifies that a method was called at least once.
- `atMost()`: Verifies that a method was called at most a certain number of times.
- `never()`: Verifies that a method was never called.

java
verify(database, times(2)).getUniqueId();
verify(database, atLeastOnce()).setUniqueId(12);
verify(database, never()).isAvailable();

Argument Matchers

You can use argument matchers like `anyInt()`, `anyString()`, or `any(YourClass.class)` if you don't care about the specific argument values.

java
verify(database).setUniqueId(anyInt());

5. Verifying No More Interactions

After verifying all expected method calls, you can use `verifyNoMoreInteractions()` to ensure no other methods were called on the mock object.

java
verifyNoMoreInteractions(database);

6. Verifying Call Order

If the order of method calls matters, you can use `InOrder` to verify the sequence.

java
InOrder inOrder = inOrder(database);
inOrder.verify(database).setUniqueId(12);
inOrder.verify(database).getUniqueId();

Example Test

Here's a complete example of a test that verifies the behavior of a mock object:

java
@Test
public void testVerify(@Mock Database database) {
    // Configure mock
    when(database.getUniqueId()).thenReturn(43);

    // Use mock in test code
    database.setUniqueId(12);
    database.getUniqueId();
    database.getUniqueId();

    // Verify method calls
    verify(database).setUniqueId(12);
    verify(database, times(2)).getUniqueId();
    verify(database, never()).isAvailable();

    // Ensure no other interactions
    verifyNoMoreInteractions(database);
}

By following these steps and techniques, you can effectively verify the behavior of mock objects in your JUnit 5 tests using Mockito.

Citations:
[1] https://www.vogella.com/tutorials/Mockito/article.html
[2] https://www.jrebel.com/blog/mock-unit-testing-with-mockito
[3] https://www.dhiwise.com/post/mastering-unit-testing-a-complete-guide-to-mockito-verify
[4] https://reflectoring.io/clean-unit-tests-with-mockito/
[5] https://www.toptal.com/java/a-guide-to-everyday-mockito
[6] https://keploy.io/blog/community/simplifying-junit-test-stubs-and-mocking
[7] https://www.digitalocean.com/community/tutorials/mockito-verify
[8] https://symflower.com/en/company/blog/2024/how-to-do-mocking-spring-boot/