如何模拟方法与无效返回类型?
我实现了一个观察者模式,但我不能用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触发。
我想展示上述系统的状态。并据此断言。
我想我已经找到了这个问题的一个更简单的答案,为一个方法调用真正的方法(即使它有一个void返回),你可以这样做:
Mockito.doCallRealMethod().when(<objectInstance>).<method>();
<objectInstance>.<method>();
或者,你可以为该类的所有方法调用real方法,这样做:
<Object> <objectInstance> = mock(<Object>.class, Mockito.CALLS_REAL_METHODS);
为这一堆问题添加另一个答案(没有双关语的意思)……
你需要调用doAnswer方法,如果你不能\不想使用间谍的。然而,你并不一定需要自己给出答案。有几个默认实现。值得注意的是,CallsRealMethods。
在实践中,它看起来像这样:
doAnswer(new CallsRealMethods()).when(mock)
.voidMethod(any(SomeParamClass.class));
Or:
doAnswer(Answers.CALLS_REAL_METHODS.get()).when(mock)
.voidMethod(any(SomeParamClass.class));
我想我已经找到了这个问题的一个更简单的答案,为一个方法调用真正的方法(即使它有一个void返回),你可以这样做:
Mockito.doCallRealMethod().when(<objectInstance>).<method>();
<objectInstance>.<method>();
或者,你可以为该类的所有方法调用real方法,这样做:
<Object> <objectInstance> = mock(<Object>.class, Mockito.CALLS_REAL_METHODS);
如何用mockito模拟void方法-有两个选项:
doAnswer -如果我们想让我们的mock void方法做一些事情(尽管它是void,但仍然模拟该行为)。
doThrow——如果你想从mock void方法抛出一个异常,那么还有Mockito.doThrow()。
下面是如何使用它的示例(不是理想的用例,只是想说明基本用法)。
@Test
public void testUpdate() {
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
if (arguments != null && arguments.length > 1 && arguments[0] != null && arguments[1] != null) {
Customer customer = (Customer) arguments[0];
String email = (String) arguments[1];
customer.setEmail(email);
}
return null;
}
}).when(daoMock).updateEmail(any(Customer.class), any(String.class));
// calling the method under test
Customer customer = service.changeEmail("old@test.com", "new@test.com");
//some asserts
assertThat(customer, is(notNullValue()));
assertThat(customer.getEmail(), is(equalTo("new@test.com")));
}
@Test(expected = RuntimeException.class)
public void testUpdate_throwsException() {
doThrow(RuntimeException.class).when(daoMock).updateEmail(any(Customer.class), any(String.class));
// calling the method under test
Customer customer = service.changeEmail("old@test.com", "new@test.com");
}
}
你可以在我的文章how to mock with Mockito中找到更多关于如何用Mockito模拟和测试void方法的细节(一个包含示例的全面指南)