1. Introduction
In this article, we will show how to configure the method call to throw an exception using Mockito. We will present two approaches: one for methods that returns some value and one for void methods - there are some differences in the implementation.
2. Example service class
We will be testing simple ThrowingService
that has two methods:
someVoidMethod(int value)
- this method doesn't return any value,someNotVoidMethod(int value)
- method that returnsint
value.
package com.frontbackend.libraries.mockito.service;
public class ThrowingService {
public void someVoidMethod(int value) {
System.out.println(1000 / value);
}
public int someNotVoidMethod(int value) {
return 10000 / value;
}
}
3. Testing void method
In the following JUnit test we show how to change the behavior of the someVoidMethod(..)
method in ThrowingService
using Mockito:
package com.frontbackend.libraries.mockito;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.doThrow;
import com.frontbackend.libraries.mockito.service.ThrowingService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class MockitoThrowsTest {
@Test(expected = IllegalArgumentException.class)
public void forVoidMethod_whenArgumentIsZero_shouldThrowIllegalArgumentException() {
ThrowingService service = Mockito.mock(ThrowingService.class);
doThrow(new IllegalArgumentException()).when(service)
.someVoidMethod(0);
service.someVoidMethod(0);
}
@Test(expected = ArithmeticException.class)
public void forVoidMethod_whenArgumentIsZeroAndCallingRealMethod_shouldThrowArithmeticException() {
ThrowingService service = Mockito.mock(ThrowingService.class);
doCallRealMethod().when(service)
.someVoidMethod(anyInt());
service.someVoidMethod(0);
}
}
In the first test we used the Mockito statement doThrow(...).when(...).method(...)
to configured someVoidMethod
to throw IllegalArgumentException
when called with argument 0
.
Note that we could not use the statement when(...).thenThrow(...)
for methods that do not return any value.
The following code WILL NOT COMPILE.
when(service.someVoidMethod(0)).thenThrow(new IllegalArgumentException()); // compilation error
4. Testing method that returns int
value
When testing not void methods we could actually decide what approache is better for us, because both will work in the same way:
when(service.someNotVoidMethod(0)).thenThrow(new IllegalArgumentException());
,doThrow(new IllegalArgumentException()).when(service).someNotVoidMethod(0);
.
In the following test class, we used the when(...).thenThrow(...)
statement to configure the not void method to throw a different exception when called with argument zero.
package com.frontbackend.libraries.mockito;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;
import com.frontbackend.libraries.mockito.service.ThrowingService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class MockitoThrowsTest {
@Test(expected = IllegalArgumentException.class)
public void forNotVoidMethod_whenArgumentIsZero_shouldThrowIllegalArgumentException() {
ThrowingService service = Mockito.mock(ThrowingService.class);
when(service.someNotVoidMethod(0)).thenThrow(new IllegalArgumentException());
service.someNotVoidMethod(0);
}
@Test(expected = ArithmeticException.class)
public void forNotVoidMethod_whenArgumentIsZeroAndCallingRealMethod_shouldThrowArithmeticException() {
ThrowingService service = Mockito.mock(ThrowingService.class);
when(service.someNotVoidMethod(anyInt())).thenCallRealMethod();
service.someNotVoidMethod(0);
}
}
5. Conclusion
In this article, we presented how to configure the method to throw an exception using the Mockito framework.
As usual, code introduced in this article is available in our GitHub repository.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}