3.1 doReturn() method
Now comes the magic...
Let's say you created a fake mock object, but you need it to work somehow. When certain methods were called, something important was done, or the methods returned a certain result. What to do?
The Mockito library allows you to add the desired behavior to a mock object.
If you want a mock object to return a certain result when a certain method is called, then this “rule” can be added to the object using the code:
Mockito.doReturn(result).when(an object).method name();
You see, at the end of the method call, method name?
there's actually no call going on here. The method doReturn()
returns a special proxy-object with the help of which it monitors the calls of the object's methods and, thus, the rule is written.
Again. It's just such a clever way to write a rule to add to a mock object . It takes some skill to correctly interpret such code in your head when you see it. Comes with experience.
I think a concrete example is needed. Let's create a mock class object ArrayList
and ask its method size()
to return the number 10. The complete code will look like this:
@ExtendWith(MockitoExtension.class)
class DoReturnTest {
@Mock
List mockList;
@Test
public void whenMockAnnotation () {
//create a rule: return 10 when calling the size method
Mockito.doReturn(10).when(mockList).size();
//the method is called here and will return 10!!
assertEquals(10, mockList.size());
}
}
Yes, this code will work, the test will not fail.
3.2 when() method
There is another way to add a behavior rule to a mock object - by calling the Mockito.when()
. It looks like this:
Mockito.when(an object.method name()).thenReturn(result);
This is the same way of writing a mock object behavior rule as the previous one. Compare:
Mockito.doReturn(result).when(an object).method name();
Here exactly the same thing happens - the construction of a new rule.
True, the first example has two minuses:
- the call is very confusing.
an object.method name()
- will not work if the method
methodname()
returnsvoid
.
Well, let's write down our favorite example usingMockito.when()
@ExtendWith(MockitoExtension.class)
class WhenTest {
@Mock
List mockList;
@Test
public void whenMockAnnotation() {
//create a rule: return 10 when calling the size method
Mockito.when(mockList.size() ).thenReturn(10);
//the method is called here and will return 10!!
assertEquals(10, mockList.size());
}
}
3.3 doThrow() method
We figured out how to make a mock object method return a specific result. How can I make it throw a specific exception? Send it to doReturn()
?
To prevent the method from returning, namely throwing an exception, you need to set the rule using the doThrow()
.
Mockito.doThrow(exception.class).when(an object).method name();
And then the second option:
Mockito.when(an object.method name()).thenThrow(exception.class);
A bit expected, right?
Well, you see, you are already starting to understand. Let's fix it with an example:
@ExtendWith(MockitoExtension.class)
class DoThrowTest {
@Mock
List mockList;
@Test
public void whenMockAnnotation() {
Mockito.when(mockList.size() ).thenThrow(IllegalStateException.class);
mockList.size(); //an exception will be thrown here
}
}
If you need to throw a specific exception object, then use the construction of the form:
Mockito.doThrow(new Exception()).when(an object).method name();
Just pass doThrow()
an exception object to the method and it will be thrown during the method call.
GO TO FULL VERSION