我有一个正在测试的方法。在它的调用堆栈中,它调用一个 DAO,实习生使用 JDBC 与 DB 聊天。我对知道 JDBC 层会发生什么并不真正感兴趣。我已经对此进行了测试,它们的效果非常好。
我正在尝试使用 DAO 层的 JMock 进行模拟,因此我可以专注于这个被测方法的细节。这是我所拥有的基本表示。
@Test
public void myTest()
{
context.checking(new Expectations() {
{
allowing(myDAO).getSet(with(any(Integer.class)));
will(returnValue(new HashSet<String>()));
}
});
// Used only to show the mock is working but not really part of this test.
// These asserts pass.
Set<String> temp = myDAO.getSet(Integer.valueOf(12));
Assert.assertNotNull(temp);
Assert.assertTrue(temp.isEmpty());
MyTestObject underTest = new MyTestObject();
// Deep in this call MyDAO is initialized and getSet() is called.
// The mock is failing to return the Set as desired. getSet() is run as
// normal and throws a NPE since JDBC is not (intentionally) setup. I want
// getSet() to just return an empty set at this layer.
underTest.thisTestMethod();
...
// Other assertions that would be helpful for this test if mocking
// was working.
}
根据我在创建此测试中所学到的知识,我无法使用 JMock 模拟间接对象。或者我没有看到关键点。我希望下半场是真的。
想法和谢谢。