Mockito
1 2 |
Mockito.mock(externalService); Mockito.reset(externalService); |
1 |
when(mock).thenReturn(value); |
1 |
when(mock).thenThrow(new IOException()); |
1 |
doReturn(result).when(mock_object).void_method_call(); |
1 |
doThrow(new IOException()).when(mock_object).void_method_call(); |
1 |
Mockito.verify(mock_object).callMethod(Matchers.eq(concrete_object)); |
Mockito Mocks vs. Spies
Mock – bare-bones shell instance
1 |
List mockedList = Mockito.mock(ArrayList.class); |
Spy – wrap an existing instance
1 |
List spyList = Mockito.spy(new ArrayList()); |
PowerMock
- final classes without interfaces
- static methods
- privates
1 2 |
@RunWith(PowerMockRunner.class) @PrepareForTest({ExternalService.class}) |
1 |
private final ExternalService externalService = PowerMockito.mock(ExternalService.class); |
1 2 |
PowerMockito.mockStatic(StaticService.class); PowerMockito.verifyStatic(); |