Not only we can stub X
method for Y
input and Z
as output, we can also stub the exceptions as well.
package com.example.mokito3.sujan;
public class AppleService {
public String processApple(String appleName) {
return "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.mock;
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.processApple("Macintosh")).thenThrow(IllegalArgumentException.class);
assertThrows(IllegalArgumentException.class, () -> appleService.processApple("Macintosh"));
}
@Test
void saveAppleWithAnnotationMockTest() {
when(appleService.processApple("Macintosh")).thenThrow(IllegalArgumentException.class);
assertThrows(IllegalArgumentException.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()
}
Mockito Exception Consecutive Stubbing
We can stub Mockito for consecutive stubbing of exception as well. ie different exceptions for consecutive calls.
package com.example.mokito3.sujan;
public class AppleService {
public String processApple(String appleName) {
return "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.mock;
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.processApple("Macintosh")).thenThrow(IllegalArgumentException.class, NullPointerException.class, ArrayIndexOutOfBoundsException.class);
assertThrows(IllegalArgumentException.class, () -> appleService.processApple("Macintosh"));
assertThrows(NullPointerException.class, () -> appleService.processApple("Macintosh"));
assertThrows(ArrayIndexOutOfBoundsException.class, () -> appleService.processApple("Macintosh"));
}
@Test
void saveAppleWithAnnotationMockTest() {
when(appleService.processApple("Macintosh")).thenThrow(IllegalArgumentException.class, NullPointerException.class, ArrayIndexOutOfBoundsException.class);
assertThrows(IllegalArgumentException.class, () -> appleService.processApple("Macintosh"));
assertThrows(NullPointerException.class, () -> appleService.processApple("Macintosh"));
assertThrows(ArrayIndexOutOfBoundsException.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()
}
Mockito Consecutive Stubbing Short Hand Notation
Mockito consecutive stubbing shorthand notation is applicable to exceptions as well.
package com.example.mokito3.sujan;
public class AppleService {
public String processApple(String appleName) {
return "I love 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.mock;
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.processApple("Macintosh")).thenThrow(IllegalArgumentException.class, NullPointerException.class, ArrayIndexOutOfBoundsException.class);
assertThrows(IllegalArgumentException.class, () -> appleService.processApple("Macintosh"));
assertThrows(NullPointerException.class, () -> appleService.processApple("Macintosh"));
assertThrows(ArrayIndexOutOfBoundsException.class, () -> appleService.processApple("Macintosh"));
}
@Test
void saveAppleWithAnnotationMockTest() {
when(appleService.processApple("Macintosh")).thenThrow(IllegalArgumentException.class, NullPointerException.class, ArrayIndexOutOfBoundsException.class);
assertThrows(IllegalArgumentException.class, () -> appleService.processApple("Macintosh"));
assertThrows(NullPointerException.class, () -> appleService.processApple("Macintosh"));
assertThrows(ArrayIndexOutOfBoundsException.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()
}
Mockito stub void method for Exception
Methods with void return type cannot be stubbed with when in case of exception, for this Mockito provides doThrow
method.
package com.example.mokito3.sujan;
public class AppleService {
public void processApple(String appleName) {
System.out.println("I love 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.ArgumentMatchers.anyString;
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(anyString());
assertThrows(RuntimeException.class, () -> appleService.processApple("Macintosh"));
}
@Test
void saveAppleWithAnnotationMockTest() {
doThrow(new RuntimeException()).when(appleService).processApple(anyString());
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()
}
Mockito stub void method for Exception
doThrow
method can be configured for continuous exception stubbing 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.ArgumentMatchers.anyString;
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(), new NullPointerException()).when(appleService).processApple(anyString());
assertThrows(RuntimeException.class, () -> appleService.processApple("Macintosh"));
assertThrows(NullPointerException.class, () -> appleService.processApple("Macintosh"));
}
@Test
void saveAppleWithAnnotationMockTest() {
doThrow(new RuntimeException(), new NullPointerException()).when(appleService).processApple(anyString());
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()
}
Mockito Consecutive Stubbing for Exception and Value
we have seen how to make consecutive stub for Mockito response values and exceptions, but if required we can combine them as well.
package com.example.mokito3.sujan;
public class AppleService {
public String processApple(String appleName) {
return "I love 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.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
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.processApple("Macintosh")).thenReturn("i eat macintosh apple").thenThrow(new RuntimeException()).thenReturn("i eat fuji apple");
assertEquals("i eat macintosh apple", appleService.processApple("Macintosh"));
assertThrows(RuntimeException.class, () -> appleService.processApple("Macintosh"));
assertEquals("i eat fuji apple", appleService.processApple("Macintosh"));
}
@Test
void saveAppleWithAnnotationMockTest() {
when(appleService.processApple("Macintosh")).thenReturn("i eat macintosh apple").thenThrow(new RuntimeException()).thenReturn("i eat fuji apple");
assertEquals("i eat macintosh apple", appleService.processApple("Macintosh"));
assertThrows(RuntimeException.class, () -> appleService.processApple("Macintosh"));
assertEquals("i eat fuji apple", 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()
}