在Mockito中,有很好的方法可以通过与模拟的交互来获得程序性的答案。例如。我们可以编程mock来返回传递给它的方法调用的参数:
when(mockDao.persist(any(Entity.class)).thenAnswer(new Answer<Entity>() {
public Entity answer(InvocationOnMock invocationOnMock) throws Throwable {
Entity entity = (Entity) invocationOnMock.getArguments()[0];
return entity;
}
});
有没有办法在Spock中做同样的事情?
答案 0 :(得分:4)
mockDao.persist(_) >> { it[0] }
或者,通过解构:
mockDao.persist(_) >> { Entity entity -> entity }