EasyMock java.lang.AssertionError:意外的方法调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28207758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:11:38  来源:igfitidea点击:

EasyMock java.lang.AssertionError: Unexpected method call

javaeasymock

提问by user2359634

I am new to EasyMock and am stuck now. Can't I set the fields of mocked object? Where am I going wrong? Any help would be really appreciated.

我是 EasyMock 的新手,现在被困住了。我不能设置模拟对象的字段吗?我哪里错了?任何帮助将非常感激。

IService service = EasyMock.createMock(IService.class);
service.setName("abc"); 
EasyMock.replay(service);
org.junit.Assert.assertEquals("abc", service.getName());
EasyMock.verify(service);

    java.lang.AssertionError: 
      Unexpected method call getName():
        setName("abc"): expected: 1, actual: 0
        at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:45)
        at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:73)
        at org.easymock.internal.ClassProxyFactory$MockMethodInterceptor.intercept(ClassProxyFactory.java:92)

回答by user2359634

I was able to set the field using expect(..) of EasyMock.

我能够使用 EasyMock 的 expect(..) 设置字段。

Remove this line of code

删除这行代码

service.setName("abc");

and add

并添加

EasyMock.expect(service.getName()).andReturn("abc");