Mockito 3 Stubber doAnswer

To stub void method in generic Answer style, doAnswer method is used.

package com.example.mokito3.sujan;

public class AppleService {
    public void processApple(String appleName) {
        System.out.println("I love " + appleName + " 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 java.util.Arrays;

import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;

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

    @Test
    void saveAppleWithStaticMockTest() {
        AppleService appleService = mock(AppleService.class);
        doAnswer(invocation -> {
            Object[] args = invocation.getArguments();
            System.out.println("args = " + Arrays.toString(args));
            Object mock = invocation.getMock();
            System.out.println("mock = " + mock);
            return null;
        }).when(appleService).processApple("Macintosh");
        appleService.processApple("Macintosh");
    }

    @Test
    void saveAppleWithAnnotationMockTest() {
        doAnswer(invocation -> {
            Object[] args = invocation.getArguments();
            System.out.println("args = " + Arrays.toString(args));
            Object mock = invocation.getMock();
            System.out.println("mock = " + mock);
            return null;
        }).when(appleService).processApple("Macintosh");
        appleService.processApple("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()
}

doAnswere Continues Exception Stub

Stubber#doAnswer method has an overloaded constructor for continuous answere stub.

package com.example.mokito3.sujan;

public class AppleService {
    public void processApple(String appleName) {
        System.out.println("I love " + appleName + " 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.invocation.InvocationOnMock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;

import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;

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

    private static Object answerA(InvocationOnMock invocation) {
        Object[] args = invocation.getArguments();
        System.out.println("answerA: args = " + Arrays.toString(args));
        Object mock = invocation.getMock();
        System.out.println("answerA: mock = " + mock);
        return null;
    }

    private static Object answerB(InvocationOnMock invocation) {
        Object[] args = invocation.getArguments();
        System.out.println("answerB: args = " + Arrays.toString(args));
        Object mock = invocation.getMock();
        System.out.println("answerB: mock = " + mock);
        return null;
    }

    @Test
    void saveAppleWithStaticMockTest() {
        AppleService appleService = mock(AppleService.class);
        doAnswer(AppleServiceTest::answerA).doAnswer(AppleServiceTest::answerB).when(appleService).processApple("Macintosh");
        appleService.processApple("Macintosh");
        appleService.processApple("Macintosh");
    }

    @Test
    void saveAppleWithAnnotationMockTest() {
        doAnswer(AppleServiceTest::answerA).doAnswer(AppleServiceTest::answerB).when(appleService).processApple("Macintosh");
        appleService.processApple("Macintosh");
        appleService.processApple("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