When using Mockito, I was always googled how to mock in this case, but I decided to summarize it because it is inefficient. I really want to use JMockit, but black magic is prohibited, so I use Mockito.
We haven't been able to move to the 2nd system yet, so Mockito1x is a prerequisite. Mockito 1.10.19 JUnit 4.11 Java 1.8.0_151
One thing to keep in mind with Mockito is that the mocking method differs depending on whether the return value of the method is void or not. Based on this, we have classified it into the following six categories.
No | Return type of mock target method | Mock range | Mock operation |
---|---|---|---|
1 | Other than void | The entire | Returns the specified return value |
2 | Other than void | The entire | Throw an exception |
3 | void | The entire | Throw an exception |
4 | Other than void | Only some methods | Returns the specified return value |
5 | Other than void | Only some methods | Throw an exception |
6 | void | Only some methods | Throw an exception |
By the way, the case where private method, static method and dependent object are created by new is within the scope of PowerMock, so I will write another article. → I wrote it. How to mock each case with PowerMock + Mockito1x https://qiita.com/taka_22/items/27ea1fbf9c305ba83dcc
No.1 /**
public method, return type is mock all except void, return value */ @Test public void test001() { List mockedList = mock(List.class);
when(mockedList.get(0)).thenReturn("aaa");
assertThat((String)mockedList.get(0), is("aaa"));
assertNull(mockedList.get(1));
}
No.2 /**
Public method, return type is void except void, all mock, throw exception */ @Test public void test002() { List mockedList = mock(List.class);
when(mockedList.get(0)).thenThrow(new RuntimeException("aaa"));
try {
mockedList.get(0);
fail();
} catch(RuntimeException e) {
assertThat(e.getMessage(), is("aaa"));
}
assertNull(mockedList.get(1));
}
No.3 The point is that doThrow comes in front.
/**
public method, return type void, all mock, throw exception */ @Test public void test003() { List mockedList = mock(List.class);
doThrow(new RuntimeException("aaa")).when(mockedList).clear();
try {
mockedList.clear();
fail();
} catch(RuntimeException e) {
assertThat(e.getMessage(), is("aaa"));
}
assertNull(mockedList.get(0));
}
No.4 If you want to mock only part of it, use spy instead of mock. It is used when you want to change the behavior of a method that is not a test target and is called from a test target method.
/**
// I added "bbb" to the real object spiedList.add("bbb"); // Spy now get (0) returns "aaa" when(spiedList.get(0)).thenReturn("aaa");
// The result is "aaa" assertThat((String)spiedList.get(0), is("aaa")); // Other methods return real results assertThat(spiedList.size(), is(1)); }
No.5 /**
// I added "bbb" to the real object spiedList.add("bbb"); // Spy now throws get (0) a RuntimeException when(spiedList.get(0)).thenThrow(new RuntimeException("aaa"));
try {
spiedList.get(0);
fail();
} catch(Exception e) {
assertThat(e.getMessage(), is("aaa"));
}
// Other methods return real results assertThat(spiedList.size(), is(1)); }
No.6 /**
// Add "bbb" to real objects spiedList.add("bbb"); // Throw a RuntimeException when clear is called doThrow(new RuntimeException("aaa")).when(spiedList).clear();
try {
spiedList.clear();
fail();
} catch(RuntimeException e) {
assertThat(e.getMessage(), is("aaa"));
}
// Other methods return real results assertThat(spiedList.size(), is(1)); assertThat((String)spiedList.get(0), is("bbb")); }
Mockito http://site.mockito.org/
https://github.com/taka2/mockito-sample
https://qiita.com/taka_22/items/27ea1fbf9c305ba83dcc
Recommended Posts