我不打算在同一项目中使用两个框架来实现相同的目的,因此我采用了Mockito 。
因此,在过去的几个月中,我一直在使用Mockito ,这是我对两者的比较分析。
public class MyApp {
MyService service;
OtherService otherService;
void operationOne() {
service.operationOne();
}
void operationTwo(String args) {
String operationTwo = otherService.operationTwo(args);
otherService.operationThree(operationTwo);
}
void operationThree() {
service.operationOne();
otherService.operationThree("success");
}
}
class MyService {
void operationOne() {}
}
class OtherService {
public String operationTwo(String args) {
return args;
}
public void operationThree(String operationTwo) {}
}
现在,让我使用EasyMock和Mockito为此类编写一个简单的测试用例。
public class MyAppEasyMockTest {
MyApp app;
MyService service;
OtherService otherService;
@Before
public void initialize() {
service = EasyMock.createMock(MyService.class);
otherService = EasyMock.createMock(OtherService.class);
app = new MyApp();
app.service = service;
app.otherService = otherService;
}
@Test
public void verifySimpleCall() {
service.operationOne();
EasyMock.replay(service);
app.operationOne();
EasyMock.verify(service);
}
}
public class MyAppMockitoTest {
MyApp app;
MyService service;
OtherService otherService;
@Before
public void initialize() {
service = Mockito.mock(MyService.class);
otherService = Mockito.mock(OtherService.class);
app = new MyApp();
app.service = service;
app.otherService = otherService;
}
@Test
public void verifySimpleCall() {
app.operationOne();
Mockito.verify(service).operationOne();
}
}
void operationOne() {
service.operationOne();
service.someOtherOp();
}
public class MyAppEasyMockTest {
@Test
public void verifyMultipleCalls() {
String args = "one";
EasyMock.expect(otherService.operationTwo(args)).andReturn(args);
otherService.operationThree(args);
EasyMock.replay(otherService);
app.operationTwo(args);
EasyMock.verify(otherService);
}
@Test(expected = RuntimeException.class)
public void verifyException() {
service.operationOne();
EasyMock.expectLastCall().andThrow(new RuntimeException());
EasyMock.replay(service);
app.operationOne();
}
@Test
public void captureArguments() {
Capture<String> captured = new Capture<String>();
service.operationOne();
otherService.operationThree(EasyMock.capture(captured));
EasyMock.replay(service, otherService);
app.operationThree();
EasyMock.verify(service, otherService);
assertTrue(captured.getValue().contains("success"));
}
}
public class MyAppMockitoTest {
@Test
public void verifyMultipleCalls() {
String args = "one";
Mockito.when(otherService.operationTwo(args)).thenReturn(args);
app.operationTwo(args);
Mockito.verify(otherService).operationTwo(args);
Mockito.verify(otherService).operationThree(args);
Mockito.verifyNoMoreInteractions(otherService);
Mockito.verifyZeroInteractions(service);
}
@Test(expected = RuntimeException.class)
public void verifyException() {
Mockito.doThrow(new RuntimeException()).when(service).operationOne();
app.operationOne();
}
@Test
public void captureArguments() {
app.operationThree();
ArgumentCaptor capturedArgs = ArgumentCaptor
.forClass(String.class);
Mockito.verify(service).operationOne();
Mockito.verify(otherService).operationThree(capturedArgs.capture());
assertTrue(capturedArgs.getValue().contains("success"));
Mockito.verifyNoMoreInteractions(service, otherService);
}
}
@RunWith(MockitoJUnitRunner.class)
public class MyAppMockitoTest {
MyApp app;
@Mock
MyService service;
@Mock
OtherService otherService;
@Before
public void initialize() {
app = new MyApp();
app.service = service;
app.otherService = otherService;
}
}
参考: 到目前为止 ,我们的JCG合作伙伴 Rahul Sharma 使用EasyMock或Mockito在The road…博客博客中。
翻译自: https://www.javacodegeeks.com/2012/08/using-easymock-or-mockito.html