java - EasyMock 2.5 不适用于多个参数捕获

标签 java mockito easymock

当我尝试使用 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));
    }
}

测试结果:

  1. mockitoTest - 总是通过
  2. easyMockTestValid - 如果在没有 easyMockTest 的情况下运行则通过
  3. 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 可能会更改代码中的某些内容以使您的代码正常工作。

希望有帮助。

关于java - EasyMock 2.5 不适用于多个参数捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10037337/

相关文章:

java - 如何在垂直盒子中正确堆叠形状? JavaFX

java - 无法从 Class<PowerMockRunner> 转换为 Class<?延伸运行者>

spring-boot - 如何模拟测试 Kotlin Spring boot 2 应用程序

java - EasyMock 和 Ibatis

java - 如何使用 EasyMock 测试内部类

java - 无法将我的头缠绕在填充一元树上

java - 如何从另一个类 (SendMail) 运行 main(String [] args)

java - 两种正则表达式模式有什么区别

java - 如何验证构造函数中的方法调用?

java - 如何使用单例实例模拟类