java EasyMock对于有参无返回至的方法怎么模拟?
答案:2 悬赏:80
解决时间 2021-02-01 01:53
- 提问者网友:很好的背叛
- 2021-01-31 07:05
无参无返回值得方法可以使用EasyMock.expectLastCall()没有问题
最佳答案
- 二级知识专家网友:爱情是怎么炼成的
- 2021-01-31 08:29
EasyMock类的文档里说了:
public static IExpectationSetters expectLastCall()
Returns the expectation setter for the last expected invocation in the
current thread. This method is used for expected invocations on void
methods.
userService.addUser(newUser1);
EasyMock.expectLastCall();
EasyMock.replay(dbMapper);
userService.addUser(newUser1);
还可以使用andAnswer:
mockObject.someMethod(eq(param1), eq(param2));
expectLastCall().andAnswer(new IAnswer() {
public Object answer() {
//supply your mock implementation here...
SomeClass arg1 = (SomeClass) getCurrentArguments()[0];
AnotherClass arg2 = (AnotherClass) getCurrentArguments()[1];
arg1.doSomething(blah);
//return the value to be returned by the method (null for void)
return null;
}
});
public static
Returns the expectation setter for the last expected invocation in the
current thread. This method is used for expected invocations on void
methods.
userService.addUser(newUser1);
EasyMock.expectLastCall();
EasyMock.replay(dbMapper);
userService.addUser(newUser1);
还可以使用andAnswer:
mockObject.someMethod(eq(param1), eq(param2));
expectLastCall().andAnswer(new IAnswer() {
public Object answer() {
//supply your mock implementation here...
SomeClass arg1 = (SomeClass) getCurrentArguments()[0];
AnotherClass arg2 = (AnotherClass) getCurrentArguments()[1];
arg1.doSomething(blah);
//return the value to be returned by the method (null for void)
return null;
}
});
全部回答
- 1楼网友:努力只為明天
- 2021-01-31 09:56
EasyMock 是你定义的一个类吗? public static void expectLastCall(){}; 当调用时EasyMock.expectLastCall() 就可以了。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯