java EasyMock对于有参无返回至的方法怎么模拟?
发布网友
发布时间:2022-07-23 11:42
我来回答
共2个回答
热心网友
时间:2024-12-11 17:35
EasyMock类的文档里说了:
public static <T> IExpectationSetters<T> 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);/* expect */
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;
}
});
热心网友
时间:2024-12-11 17:35
EasyMock 是你定义的一个类吗? public static void expectLastCall(){}; 当调用时EasyMock.expectLastCall() 就可以了。