Mockito 3 VerificationMode

Verify provides us a way to validate that the mock object is working and the required method was called. but what if we want to know how many times interaction happened or the no of times method has been called or in case the method called is based on parameter value then whether the required method is called or not. to validate these conditions, Mockito provides us an overloaded verify method that takes VerificationMode as an input. some of the methods of VerificationMode are.

  • times(count)
  • never()
  • atLeastOnce()
  • atLeast(count)
  • atMost(count)
  • only()
  • call(count)

times

times method provided in VerificationMode is to make sure that the desired method is called fixed no of times only. if we don't provide any VerificationMode.times parameter, Mockito by default consider it VerificationMode#times(1).

In below code processApple method calls either of saveApple or saveNullApple based on the input parameter. if saveApple is called then saveNullApple will not and vice versa.

package com.example.mokito3.sujan;

public class AppleService {
    public String saveApple(String apple) {
        System.out.println("i love " + apple + " apple");
        return apple;
    }

    public String processApple(String apple) {
        System.out.println("i eat " + apple + " apple");
        return apple;
    }
}
package com.example.mokito3.sujan;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class AppleServiceTest {
    @Mock
    private AppleService appleService;

    @Test
    void processAppleMockTest() {
        when(appleService.processApple("Macintosh")).thenReturn("i have apple");
        String apple = appleService.processApple("Macintosh");
        verify(appleService, times(1)).processApple("Macintosh");
        verify(appleService, times(0)).saveApple("Macintosh");
        assertEquals("i have apple", apple);
    }

    @Test
    void saveAppleMockTest() {
        when(appleService.saveApple("Macintosh")).thenReturn("i have apple");
        String apple = appleService.saveApple("Macintosh");
        verify(appleService, times(1)).saveApple("Macintosh");
        verify(appleService, times(0)).processApple("Macintosh");
        assertEquals("i have apple", apple);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories { jcenter() }
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
    testCompile 'org.mockito:mockito-junit-jupiter:3.4.4'
}
test {
    useJUnitPlatform()
}

never and times(0)

never method provided in VerificationMode is to make sure that the desired method is never called. if we don't provide any VerificationMode.never parameter, Mockito by default consider it VerificationMode.times(0).

In below code processApple method calls either of saveApple or saveNullApple based on the input parameter. if saveApple is called then saveNullApple will not and vice versa.

package com.example.mokito3.sujan;

public class AppleService {
    public String saveApple(String apple) {
        System.out.println("i love " + apple + " apple");
        return apple;
    }

    public String processApple(String apple) {
        System.out.println("i eat " + apple + " apple");
        return apple;
    }
}
package com.example.mokito3.sujan;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class AppleServiceTest {
    @Mock
    private AppleService appleService;

    @Test
    void processAppleMockTest() {
        when(appleService.processApple("Macintosh")).thenReturn("i have apple");
        String apple = appleService.processApple("Macintosh");
        verify(appleService, times(1)).processApple("Macintosh");
        verify(appleService, times(0)).saveApple("Macintosh");
        verify(appleService, never()).saveApple("Macintosh");
        assertEquals("i have apple", apple);
    }

    @Test
    void saveAppleMockTest() {
        when(appleService.saveApple("Macintosh")).thenReturn("i have apple");
        String apple = appleService.saveApple("Macintosh");
        verify(appleService, times(1)).saveApple("Macintosh");
        verify(appleService, times(0)).processApple("Macintosh");
        verify(appleService, never()).processApple("Macintosh");
        assertEquals("i have apple", apple);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories { jcenter() }
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
    testCompile 'org.mockito:mockito-junit-jupiter:3.4.4'
}
test {
    useJUnitPlatform()
}

atLeastOnce and atLeast()

atLeastOnce the method provided in VerificationMode is to make sure that the desired method is called multiple times or at least 1 time. it is equivalent to VerificationMode.atLeast(1) parameter.

In below code processApple method calls either of saveApple or saveNullApple based on the input parameter. So for both test case are per input passed saveApple and saveNullApple will be called at least once.

package com.example.mokito3.sujan;

public class AppleService {
    public String saveApple(String apple) {
        System.out.println("i love " + apple + " apple");
        return apple;
    }

    public String processApple(String apple) {
        System.out.println("i eat " + apple + " apple");
        return apple;
    }
}
package com.example.mokito3.sujan;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class AppleServiceTest {
    @Mock
    private AppleService appleService;

    @Test
    void processAppleMockTest() {
        when(appleService.processApple("Macintosh")).thenReturn("i have apple");
        String apple = appleService.processApple("Macintosh");
        verify(appleService, times(1)).processApple("Macintosh");
        verify(appleService, times(0)).saveApple("Macintosh");
        verify(appleService, atLeastOnce()).processApple("Macintosh");
        verify(appleService, atLeast(1)).processApple("Macintosh");
        assertEquals("i have apple", apple);
    }

    @Test
    void saveAppleMockTest() {
        when(appleService.saveApple("Macintosh")).thenReturn("i have apple");
        String apple = appleService.saveApple("Macintosh");
        verify(appleService, times(1)).saveApple("Macintosh");
        verify(appleService, times(0)).processApple("Macintosh");
        assertEquals("i have apple", apple);
        verify(appleService, atLeastOnce()).saveApple("Macintosh");
        verify(appleService, atLeast(1)).saveApple("Macintosh");
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories { jcenter() }
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
    testCompile 'org.mockito:mockito-junit-jupiter:3.4.4'
}
test {
    useJUnitPlatform()
}

 

atMostOnce and atMost()

atMostOnce the method provided in VerificationMode is to make sure that the desired method is called never or at most 1 time only. it is equivalent to VerificationMode.atMost(1) parameter.

In below code processApple method calls either of saveApple or saveNullApple based on the input parameter. So for both test case are per input passed saveApple and saveNullApple will be called at least once.

package com.example.mokito3.sujan;

public class AppleService {
    public String saveApple(String apple) {
        System.out.println("i love " + apple + " apple");
        return apple;
    }

    public String processApple(String apple) {
        System.out.println("i eat " + apple + " apple");
        return apple;
    }
}
package com.example.mokito3.sujan;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.atMost;
import static org.mockito.Mockito.atMostOnce;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class AppleServiceTest {
    @Mock
    private AppleService appleService;

    @Test
    void processAppleMockTest() {
        when(appleService.processApple("Macintosh")).thenReturn("i have apple");
        String apple = appleService.processApple("Macintosh");
        verify(appleService, times(1)).processApple("Macintosh");
        verify(appleService, times(0)).saveApple("Macintosh");
        verify(appleService, atMostOnce()).saveApple("Macintosh");
        verify(appleService, atMost(1)).saveApple("Macintosh");
        assertEquals("i have apple", apple);
    }

    @Test
    void saveAppleMockTest() {
        when(appleService.saveApple("Macintosh")).thenReturn("i have apple");
        String apple = appleService.saveApple("Macintosh");
        verify(appleService, times(1)).saveApple("Macintosh");
        verify(appleService, times(0)).processApple("Macintosh");
        assertEquals("i have apple", apple);
        verify(appleService, atMostOnce()).saveApple("Macintosh");
        verify(appleService, atMost(1)).saveApple("Macintosh");
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories { jcenter() }
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
    testCompile 'org.mockito:mockito-junit-jupiter:3.4.4'
}
test {
    useJUnitPlatform()
}

 

only

A mock object may contain lots of methods, but if we want to verify that interaction has been done with 1 method only, for this Mockito provides us VerificationMode#only method.

package com.example.mokito3.sujan;

public class AppleService {
    public String saveApple(String apple) {
        String appleString = "i love " + apple + " apple";
        return appleString;
    }

    public String getApple(String apple) {
        String appleString = "i eat " + apple + " apple";
        return appleString;
    }
}
package com.example.mokito3.sujan;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class AppleServiceTest {
    @Mock
    private AppleService appleService;

    @Test
    void processAppleMockTest() {
        when(appleService.processApple("Macintosh")).thenReturn("i have apple");
        String apple = appleService.processApple("Macintosh");
        verify(appleService, times(1)).processApple("Macintosh");
        verify(appleService, times(0)).saveApple("Macintosh");
        verify(appleService, only()).processApple("Macintosh");
        verify(appleService, atLeast(1)).processApple("Macintosh");
        assertEquals("i have apple", apple);
    }

    @Test
    void saveAppleMockTest() {
        when(appleService.saveApple("Macintosh")).thenReturn("i have apple");
        String apple = appleService.saveApple("Macintosh");
        verify(appleService, times(1)).saveApple("Macintosh");
        verify(appleService, times(0)).processApple("Macintosh");
        verify(appleService, only()).saveApple("Macintosh");
        verify(appleService, atLeast(1)).saveApple("Macintosh");
        assertEquals("i have apple", apple);
    }
}
plugins {
    id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'

repositories { jcenter() }
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
    testCompile 'org.mockito:mockito-junit-jupiter:3.4.4'
}
test {
    useJUnitPlatform()
}

 

call

The call method provided in VerificationMode is equivalent to times method but with relaxed strictness. VerificationMode#times method make sure that the stub method has been invoked for fixed no of times but will fail if the method invoked more verified no of invocation. call method can only be used with inorder.

appleService.saveApple("Macintosh");
appleService.saveApple("Macintosh");
appleService.saveApple("Macintosh");
inOrder.verify(appleService, times( 2 )).saveApple("Macintosh");

but the call method will not mark it as a failure.

package com.example.mokito3.sujan;

public class AppleService {
    public String saveApple(String apple) {
        System.out.println("i love " + apple + " apple");
        return apple;
    }
}
package com.example.mokito3.sujan;

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

import static org.mockito.Mockito.calls;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;

@ExtendWith(MockitoExtension.class)
public class AppleServiceTest {
    @Mock
    private AppleService appleService;

    @Test
    void saveAppleWithStaticMockTest() {
        AppleService appleService = mock(AppleService.class);
        InOrder inOrder = inOrder(appleService);
        appleService.saveApple("Macintosh");
        appleService.saveApple("Macintosh");
        appleService.saveApple("Macintosh");
        inOrder.verify(appleService, calls(2)).saveApple("Macintosh");
    }

    @Test
    void saveAppleWithAnnotationMockTest() {
        InOrder inOrder = inOrder(appleService);
        appleService.saveApple("Macintosh");
        appleService.saveApple("Macintosh");
        appleService.saveApple("Macintosh");
        inOrder.verify(appleService, calls(2)).saveApple("Macintosh");
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories { jcenter() }
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
    testCompile 'org.mockito:mockito-junit-jupiter:3.4.4'
}
test {
    useJUnitPlatform()
}

follow us on