我想模拟构建器模式中使用的 DAO 接口(interface),如下所示。但是,当我运行下面的测试时,它通过了,表明我的模拟对象从未被调用过。我做错了什么?
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 对象。
请引用this学习如何使用 PowerMockito。
关于java - 使用 Mockito 对流畅的界面进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30823858/