Verify Behaviour of your services with Mockity verify()

In this example we explain how to use Mockito verify to verify complex behaviour of our services. Here is a list of what we can verify:

  • Verify some behaviour
  • Stub expected values
  • Verifying exact number of invocations
  • Verification in order
  • Making sure interaction(s) never happened on mock
  • Finding redundant invocations

Dependencies

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.memorynotfound.test.mockito</groupId>
    <artifactId>verify</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>MOCKITO - ${project.artifactId}</name>
    <url>http://memorynotfound.com</url>

    <properties>
        <mockito.version>1.10.19</mockito.version>
        <junit.version>4.12</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>${mockito.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

Mockable Services

Lets assume we have these two very basic services which we want to mock. Normally you would mock some unpredicable API’s which don’t always give you the correct data every time.

Message ‘service’

package memorynotfound;

public class Message {
    
    public boolean hasMessages(){
        return true;
    }
}

User ‘service’

package memorynotfound;

public class User {

    public boolean isLocked(){
        return false;
    }
}

Mockito verify method

package com.memorynotfound.test;

import memorynotfound.Message;
import memorynotfound.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;

/**
 * Unit Testing with Mockito Verify
 */
@RunWith(MockitoJUnitRunner.class)
public class VerifyTest {

    @Mock Message message;
    @Mock User user;

    @Test
    public void testVerifyTimesX(){
        when(message.hasMessages()).thenReturn(true);
        assertTrue(message.hasMessages());
        assertTrue(message.hasMessages());
        assertTrue(message.hasMessages());
        verify(message, times(3)).hasMessages();
    }

    @Test
    public void testAtLeast(){
        when(message.hasMessages()).thenReturn(true);
        assertTrue(message.hasMessages());
        assertTrue(message.hasMessages());
        verify(message, atLeast(1)).hasMessages();
    }

    @Test
    public void testOnly(){
        when(message.hasMessages()).thenReturn(true);
        assertTrue(message.hasMessages());
        verify(message, only()).hasMessages();
    }

    @Test
    public void testAfter(){
        when(message.hasMessages()).thenReturn(true);
        assertTrue(message.hasMessages());
        verify(message, after(0)).hasMessages();
    }

    @Test
    public void testAtMost(){
        when(message.hasMessages()).thenReturn(true);
        assertTrue(message.hasMessages());
        verify(message, atMost(1)).hasMessages();
    }

    @Test
    public void testNoMoreInteractions(){
        when(message.hasMessages()).thenReturn(true);
        assertTrue(message.hasMessages());
        verify(message).hasMessages();
        verifyNoMoreInteractions(message);
    }

    @Test
    public void testZeroInteractions(){
        verifyZeroInteractions(message, user);
    }

    @Test
    public void testInOrder(){
        InOrder order = inOrder(message, user);

        when(message.hasMessages()).thenReturn(true);
        when(user.isLocked()).thenReturn(false);

        assertTrue(message.hasMessages());
        assertFalse(user.isLocked());
        assertFalse(user.isLocked());

        order.verify(message, calls(1)).hasMessages();
        order.verify(user, calls(2)).isLocked();
    }
}

You may also like...