2

我想模拟构建器模式中使用的 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);
    }
}
4

1 回答 1

1

请改用 PowerMockito。在那里,您可以定义每当您调用 DAO 的构造函数时,返回我的模拟对象而不是返回实际的 DAO 对象。
请参考这里了解如何使用 PowerMockito。

于 2015-06-14T07:33:36.757 回答