当我尝试使用EasyMock捕获多个值时出现异常。
easymock 2.5.2
easymockclassextension 2.2
模拟所有1.8.5
hamcrest-all 1.1
如何使用EasyMock解决它?
初始代码:
package easymock;
public class User {
public static final int INT_VALUE = 1;
public static final boolean BOOLEAN_VALUE = false;
public static final String STRING_VALUE = "";
private Service service;
public void setService(Service service) {
this.service = service;
}
public String userMethod(){
return service.doSomething(INT_VALUE, BOOLEAN_VALUE, STRING_VALUE);
}
}
package easymock;
public class Service {
public String doSomething(Integer a, boolean b, String c){
return null;
}
}
package easymock;
import org.easymock.Capture;
import org.easymock.classextension.EasyMock;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import static org.easymock.EasyMock.anyBoolean;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.classextension.EasyMock.capture;
import static org.easymock.classextension.EasyMock.expect;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class UserTest {
private User user;
private Service easyMockNiceMock;
private Service mockitoMock;
@Before
public void setUp() throws Exception {
user = new User();
easyMockNiceMock = EasyMock.createNiceMock(Service.class);
mockitoMock = mock(Service.class);
}
@Test
public void easyMockTest() throws Exception {
// given
user.setService(easyMockNiceMock);
Capture<Integer> integerCapture = new Capture<Integer>();
Capture<Boolean> booleanCapture = new Capture<Boolean>();
Capture<String> stringCapture = new Capture<String>();
expect(easyMockNiceMock.doSomething(capture(integerCapture), capture(booleanCapture), capture(stringCapture))).andReturn("");
replay(easyMockNiceMock);
// when
user.userMethod();
// then
verify(easyMockNiceMock);
assertThat(integerCapture.getValue(), is(User.INT_VALUE));
assertThat(booleanCapture.getValue(), is(User.BOOLEAN_VALUE));
assertThat(stringCapture.getValue(), is(User.STRING_VALUE));
}
@Test
public void easyMockTestValid() throws Exception {
// given
user.setService(easyMockNiceMock);
Capture<Integer> integerCapture = new Capture<Integer>();
expect(easyMockNiceMock.doSomething(capture(integerCapture), anyBoolean(), (String) anyObject())).andReturn("");
replay(easyMockNiceMock);
// when
user.userMethod();
// then
verify(easyMockNiceMock);
assertThat(integerCapture.getValue(), is(User.INT_VALUE));
}
@Test
public void mockitoTest() throws Exception {
// given
user.setService(mockitoMock);
ArgumentCaptor<Integer> integerArgumentCaptor = ArgumentCaptor.forClass(Integer.class);
ArgumentCaptor<Boolean> booleanArgumentCaptor = ArgumentCaptor.forClass(Boolean.class);
ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
when(mockitoMock.doSomething(integerArgumentCaptor.capture(), booleanArgumentCaptor.capture(), stringArgumentCaptor.capture())).thenReturn("");
// when
user.userMethod();
// then
assertThat(integerArgumentCaptor.getValue(), is(User.INT_VALUE));
assertThat(booleanArgumentCaptor.getValue(), is(User.BOOLEAN_VALUE));
assertThat(stringArgumentCaptor.getValue(), is(User.STRING_VALUE));
}
}
检测结果:
mockitoTest-始终通过
easyMockTestValid-如果在没有easyMockTest的情况下运行,则通过
easyMockTest-总是失败,并显示以下消息:
java.lang.NullPointerException
at easymock.UserTest.easyMockTest(UserTest.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
最佳答案
首先,您使用的是EasyMock和EasyMock类扩展的不同版本。我不认为这是您的问题,但我建议您使用一致的版本。
其次,我在EasyMock的3.1版中尝试了您的代码,不再需要类扩展,并且测试通过了。我在变更日志中没有看到任何有趣的东西,但是Henri可能会更改代码中的某些内容,从而使您的代码正常工作。
希望能有所帮助。