如何模拟方法与无效返回类型?
我实现了一个观察者模式,但我不能用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触发。
我想展示上述系统的状态。并据此断言。
如何用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方法的细节(一个包含示例的全面指南)
我想我已经找到了这个问题的一个更简单的答案,为一个方法调用真正的方法(即使它有一个void返回),你可以这样做:
Mockito.doCallRealMethod().when(<objectInstance>).<method>();
<objectInstance>.<method>();
或者,你可以为该类的所有方法调用real方法,这样做:
<Object> <objectInstance> = mock(<Object>.class, Mockito.CALLS_REAL_METHODS);
如果你需要在mock void方法中做一些操作,你需要操纵发送给void方法的参数;你可以结合Mockito。doAnswer与ArgumentCaptor。捕捉方法。
假设你有一个自动连接GalaxyService的SpaceService,它有一个叫做someServiceMethod的void方法。
您需要为SpaceService中的一个方法编写测试,该方法调用了GalaxyService的void方法。你的星球也是在宇宙服务内部生成的。所以你没有机会嘲笑我。
下面是您希望为其编写测试的示例SpaceService类。
class SpaceService {
@Autowired
private GalaxyService galaxyService;
public Date someCoolSpaceServiceMethod() {
// does something
Planet planet = new World();
galaxyService.someServiceMethod(planet); //Planet updated in this method.
return planet.getCurrentTime();
}
}
GalaxyService。someServiceMethod方法需要一个行星参数。在方法中做一些事情。看到的:
GalaxyService {
public void someServiceMethod(Planet planet) {
//do fancy stuff here. about solar system etc.
planet.setTime(someCalculatedTime); // the thing that we want to test.
// some more stuff.
}
}
你想测试这个功能。
这里有一个例子:
ArgumentCaptor<World> worldCaptor = ArgumentCaptor.forClass(World.class);
Date testDate = new Date();
Mockito.doAnswer(mocked-> {
World capturedWorld = worldCaptor.getValue();
world.updateTime(testDate);
return null;
}).when(galaxyService.someServiceMethod(worldCaptor.capture());
Date result = spaceService.someCoolSpaceServiceMethod();
assertEquals(result, testDate);
如何用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方法的细节(一个包含示例的全面指南)