Mockito 3 Custom verification failure message

Mockito framework provides very descriptive messages for success and failure cases. But if required Mockito allows adding custom verification failure messages as well.

package com.example.mokito3.sujan;

public abstract class AppleService {
    public String saveApple(String apple) {
        String appleString = "i love " + apple + " apple";
        return appleString;
    }
}
package com.example.mokito3.sujan;

import org.junit.jupiter.api.Assertions;
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.mockito.Mockito.description;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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

    @Test
    void saveAppleWithStaticMockTest() {
        AppleService appleService = mock(AppleService.class);
        when(appleService.saveApple("Macintosh")).thenReturn("i eat apple");
        String apple = appleService.saveApple("Macintosh");
        Assertions.assertEquals("i eat apple", apple);
        verify(appleService, description("appleService#saveApple is not called with Fuji parameter")).saveApple("Fuji");
    }

    @Test
    void saveAppleWithAnnotationMockTest() {
        when(appleService.saveApple("Macintosh")).thenReturn("i eat apple");
        String apple = appleService.saveApple("Macintosh");
        Assertions.assertEquals("i eat apple", apple);
        verify(appleService, description("appleService#saveApple is not called with Fuji parameter")).saveApple("Fuji");
    }
}
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()
}

Mockito custom verification failure messages can be added with VerificationMode as well.

package com.example.mokito3.sujan;

public abstract class AppleService {
    public String saveApple(String apple) {
        String appleString = "i love " + apple + " apple";
        return appleString;
    }
}
package com.example.mokito3.sujan;

import org.junit.jupiter.api.Assertions;
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.mockito.Mockito.mock;
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 saveAppleWithStaticMockTest() {
        AppleService appleService = mock(AppleService.class);
        when(appleService.saveApple("Macintosh")).thenReturn("i eat apple");
        String apple = appleService.saveApple("Macintosh");
        Assertions.assertEquals("i eat apple", apple);
        verify(appleService, times(2).description("appleService#saveApple must be invoked atlest twice")).saveApple("Macintosh");
    }

    @Test
    void saveAppleWithAnnotationMockTest() {
        when(appleService.saveApple("Macintosh")).thenReturn("i eat apple");
        String apple = appleService.saveApple("Macintosh");
        Assertions.assertEquals("i eat apple", apple);
        verify(appleService, times(2).description("appleService#saveApple must be invoked atlest twice")).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