テスト中のメソッドがあります。コールスタック内で、インターンがJDBCを使用してDBとチャットするDAOを呼び出します。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を使用して間接オブジェクトをモックすることはできません。または、重要なポイントが表示されていません。後半が真実になることを願っています。
考えていただきありがとうございます。