Mockito 3 Stubber doThrow

To stub exception in partial mock or void method, doThrow 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 static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;

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

    @Test
    void saveAppleWithStaticMockTest() {
        AppleService appleService = mock(AppleService.class);
        doThrow(new RuntimeException()).when(appleService).processApple("Macintosh");
        assertThrows(RuntimeException.class, () -> appleService.processApple("Macintosh"));
    }

    @Test
    void saveAppleWithAnnotationMockTest() {
        doThrow(new RuntimeException()).when(appleService).processApple("Macintosh");
        assertThrows(RuntimeException.class, () -> 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()
}

do throw Continues Exception Stub

Stubber#doThrow method has an overloaded constructor for continuous exception stub. creating a continues stub with a new Exception instance allows us to pass exception messages as well.

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

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

    @Test
    void saveAppleWithStaticMockTest() {
        AppleService appleService = mock(AppleService.class);
        doThrow(new RuntimeException("oops RuntimeException"), new NullPointerException("oops NullPointerException")).when(appleService).processApple("Macintosh");
        assertThrows(RuntimeException.class, () -> appleService.processApple("Macintosh"));
        assertThrows(NullPointerException.class, () -> appleService.processApple("Macintosh"));
    }

    @Test
    void saveAppleWithAnnotationMockTest() {
        doThrow(new RuntimeException("oops RuntimeException"), new NullPointerException("oops NullPointerException")).when(appleService).processApple("Macintosh");
        assertThrows(RuntimeException.class, () -> appleService.processApple("Macintosh"));
        assertThrows(NullPointerException.class, () -> 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()
}

 

Stubber doThrow with Exception class

If we don't require to pass exception messages then we can use doThrow method with the exception class.

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

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

    @Test
    void saveAppleWithStaticMockTest() {
        AppleService appleService = mock(AppleService.class);
        doThrow(RuntimeException.class, NullPointerException.class).when(appleService).processApple("Macintosh");
        assertThrows(RuntimeException.class, () -> appleService.processApple("Macintosh"));
        assertThrows(NullPointerException.class, () -> appleService.processApple("Macintosh"));
    }

    @Test
    void saveAppleWithAnnotationMockTest() {
        doThrow(RuntimeException.class, NullPointerException.class).when(appleService).processApple("Macintosh");
        assertThrows(RuntimeException.class, () -> appleService.processApple("Macintosh"));
        assertThrows(NullPointerException.class, () -> 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