easyMockとMockitoの対比
EasyMock import static org.easymock.classextension.EasyMock.*;
List mock = createNiceMock(List.class);
expect(mock.get(0)).andStubReturn("one");
expect(mock.get(1)).andStubReturn("two");
mock.clear();
replay(mock);
someCodeThatInteractsWithMock();
verify(mock);
Mockito import static org.mockito.Mockito.*;
List mock = mock(List.class);
when(mock.get(0)).thenReturn("one");
when(mock.get(1)).thenReturn("two");
someCodeThatInteractsWithMock();
verify(mock).clear();
Similarities
import static org.easymock.classextension.EasyMock.*;
List mock = createNiceMock(List.class);
expect(mock.get(0)).andStubReturn("one");
expect(mock.get(1)).andStubReturn("two");
mock.clear();
replay(mock);
someCodeThatInteractsWithMock();
verify(mock);
import static org.mockito.Mockito.*;
List mock = mock(List.class);
when(mock.get(0)).thenReturn("one");
when(mock.get(1)).thenReturn("two");
someCodeThatInteractsWithMock();
verify(mock).clear();
Similarities
Differences
Verification in order
EasyMock
Control control = createStrictControl();
List one = control.createMock(List.class);
List two = control.createMock(List.class);
expect(one.add("one")).andReturn(true);
expect(two.add("two")).andReturn(true);
control.replay();
someCodeThatInteractsWithMocks();
control.verify();
Mockito
List one = mock(List.class);
List two = mock(List.class);
someCodeThatInteractsWithMocks();
InOrder inOrder = inOrder(one, two);
inOrder.verify(one).add("one");
inOrder.verify(two).add("two");
Stubbing void methods
EasyMock
List mock = createNiceMock(List.class);
mock.clear();
expectLastCall().andThrow(new RuntimeException());
replay(mock);
Mockito
List mock = mock(List.class);
doThrow(new RuntimeException()).when(mock).clear();
Exact number of times verification and argument matchers
EasyMock
List mock = createNiceMock(List.class);
mock.clear();
expectLastCall().times(3);
expect(mock.add(anyObject())).andReturn(true).atLeastOnce();
replay(mock);
someCodeThatInteractsWithMock();
verify(mock);
Mockito
List mock = mock(List.class);
someCodeThatInteractsWithMock();
verify(mock, times(3)).clear();
verify(mock, atLeastOnce()).add(anyObject());
Control control = createStrictControl();
List one = control.createMock(List.class);
List two = control.createMock(List.class);
expect(one.add("one")).andReturn(true);
expect(two.add("two")).andReturn(true);
control.replay();
someCodeThatInteractsWithMocks();
control.verify();
List one = mock(List.class);
List two = mock(List.class);
someCodeThatInteractsWithMocks();
InOrder inOrder = inOrder(one, two);
inOrder.verify(one).add("one");
inOrder.verify(two).add("two");
EasyMock
List mock = createNiceMock(List.class);
mock.clear();
expectLastCall().andThrow(new RuntimeException());
replay(mock);
Mockito
List mock = mock(List.class);
doThrow(new RuntimeException()).when(mock).clear();
Exact number of times verification and argument matchers
EasyMock
List mock = createNiceMock(List.class);
mock.clear();
expectLastCall().times(3);
expect(mock.add(anyObject())).andReturn(true).atLeastOnce();
replay(mock);
someCodeThatInteractsWithMock();
verify(mock);
Mockito
List mock = mock(List.class);
someCodeThatInteractsWithMock();
verify(mock, times(3)).clear();
verify(mock, atLeastOnce()).add(anyObject());
List mock = createNiceMock(List.class);
mock.clear();
expectLastCall().times(3);
expect(mock.add(anyObject())).andReturn(true).atLeastOnce();
replay(mock);
someCodeThatInteractsWithMock();
verify(mock);
List mock = mock(List.class);
someCodeThatInteractsWithMock();
verify(mock, times(3)).clear();
verify(mock, atLeastOnce()).add(anyObject());