如何模拟方法与无效返回类型?
我实现了一个观察者模式,但我不能用Mockito模拟它,因为我不知道怎么做。
我试图在网上找到一个例子,但没有成功。
我的类是这样的:
public class World {
List<Listener> listeners;
void addListener(Listener item) {
listeners.add(item);
}
void doAction(Action goal,Object obj) {
setState("i received");
goal.doAction(obj);
setState("i finished");
}
private string state;
//setter getter state
}
public class WorldTest implements Listener {
@Test public void word{
World w= mock(World.class);
w.addListener(this);
...
...
}
}
interface Listener {
void doAction();
}
系统不会被mock触发。
我想展示上述系统的状态。并据此断言。
为这一堆问题添加另一个答案(没有双关语的意思)……
你需要调用doAnswer方法,如果你不能\不想使用间谍的。然而,你并不一定需要自己给出答案。有几个默认实现。值得注意的是,CallsRealMethods。
在实践中,它看起来像这样:
doAnswer(new CallsRealMethods()).when(mock)
.voidMethod(any(SomeParamClass.class));
Or:
doAnswer(Answers.CALLS_REAL_METHODS.get()).when(mock)
.voidMethod(any(SomeParamClass.class));
首先:你应该总是导入mockito static,这样代码将更易于阅读(和直观):
import static org.mockito.Mockito.*;
对于部分嘲笑,仍然保持原始功能上的休息模拟提供“间谍”。
你可以这样使用它:
private World world = spy(new World());
要消除一个正在执行的方法,你可以使用这样的方法:
doNothing().when(someObject).someMethod(anyObject());
给一个方法一些自定义行为,使用"when"和"thenReturn":
doReturn("something").when(this.world).someMethod(anyObject());
对于更多的例子,请在文档中找到优秀的模拟样品。
在Java 8中,假设你有一个org.mockito.Mockito.doAnswer的静态导入,这可以做得更清楚一些:
doAnswer(i -> {
// Do stuff with i.getArguments() here
return null;
}).when(*mock*).*method*(*methodArguments*);
返回null;是重要的,如果没有它,编译将失败,并出现一些相当模糊的错误,因为它将无法为doAnswer找到合适的覆盖。
例如,ExecutorService可以立即执行传递给execute()的任何可运行对象,可以使用以下方法实现:
doAnswer(i -> {
((Runnable) i.getArguments()[0]).run();
return null;
}).when(executor).execute(any());
看一下Mockito API文档。正如链接文档中提到的(第12点),你可以使用Mockito框架中的doThrow()、doAnswer()、doNothing()、doReturn()方法家族中的任何一个来模拟void方法。
例如,
Mockito.doThrow(new Exception()).when(instance).methodName();
或者如果你想把它和后续行为结合起来,
Mockito.doThrow(new Exception()).doNothing().when(instance).methodName();
假设您正在下面的类World中模拟setter setState(String s),代码使用doAnswer方法来模拟setState。
World mockWorld = mock(World.class);
doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
System.out.println("called with arguments: " + Arrays.toString(args));
return null;
}
}).when(mockWorld).setState(anyString());
我想我已经找到了这个问题的一个更简单的答案,为一个方法调用真正的方法(即使它有一个void返回),你可以这样做:
Mockito.doCallRealMethod().when(<objectInstance>).<method>();
<objectInstance>.<method>();
或者,你可以为该类的所有方法调用real方法,这样做:
<Object> <objectInstance> = mock(<Object>.class, Mockito.CALLS_REAL_METHODS);