[英]Unit testing a fluent interface with Mockito
我想模拟构建器模式中使用的 DAO 接口,如下所示。 但是,当我运行下面的测试时,它通过,表明我的模拟对象从未被调用。 我究竟做错了什么?
public class DBContent {
...
public static class Builder {
DAO dao = new DAO();
...
public Builder callInsert() {
...
long latest = dao.insert();
...
}
}
...
}
@RunWith(MockitoJUnitRunner.class)
public class DBContentTest {
@Mock
DAO dao;
@Test
public void test() {
when(dao.insert()).thenReturn(1111L);
DBContent db = DBContent.db()
.callInsert()
.callInsert()
.callInsert()
.build();
verifyZeroInteractions(dao);
}
}
请改用 PowerMockito。 在那里你可以定义每当你调用 DAO 的构造函数时,返回我的模拟对象而不是返回实际的 DAO 对象。
请参阅此以了解如何使用 PowerMockito。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.