我有以下(简化的)函数,我希望使用 JUnit 检查它:
protected IDfCollection getCollection(IDfSession session) throws DfException{
IDfQuery dfcQuery = new DfQuery();
dfcQuery.setDQL(MY_DQL);
return dfcQuery.execute(session, IDfQuery.READ_QUERY);
}
我已经使用真实的 IDfSession 成功测试了它,但我想在不连接到存储库的情况下进行测试。所以我尝试使用以下方法模拟空 IDfSession:
IDfSession mockedSession = Mockito.mock(IDfSession.class);
但是我得到了 NullPointerException:
Caused by: java.lang.NullPointerException
at java.util.StringTokenizer.<init>(StringTokenizer.java:182)
at java.util.StringTokenizer.<init>(StringTokenizer.java:204)
at com.documentum.fc.internal.util.SoftwareVersion.<init>(SoftwareVersion.java:53)
at com.documentum.fc.client.DfQuery.runQuery(DfQuery.java:136)
at com.documentum.fc.client.DfQuery.execute(DfQuery.java:208)
不知道到底出了什么问题(模拟对象的哪个函数返回了 null,这不是预期的),我创建了一个实现 IDfSession 接口(interface)的简单类,并使用代码覆盖工具来检查调用了哪个函数。我希望稍后使用mockito 来模拟该函数的行为。我似乎是getServerVersion
,所以我将返回的null更改为真实值“6.5.0.355 SP3P0600 Linux.Oracle”
。下一个调用的函数是 getBatchManager,所以我也在这里模拟了返回的对象。但现在我明白了:
Caused by: java.lang.ClassCastException: com.example.model.mock.IDfSessionMocked cannot be cast to com.documentum.fc.client.impl.session.ISession
我尝试在 IDfSessionMocked 类中实现 ISession 接口(interface),但它无法编译,例如因为使用的类型之一(即 com.documentum.fc.client.impl.session.ISessionListener)不可见。
这里:http://www.informedconsulting.nl/blog/?p=187我找到了如何使用 powerMock 执行此操作的信息。另一个区别是对象是直接从 session 中获取的,而不是使用 IDfQuery。
我应该做什么?
评论后更新
getBatchManager 函数被模拟,现在它返回匿名内部类对象,所有返回值设置为 false 或 0,具体取决于预期的返回类型。根据覆盖率工具已调用函数isFlushBatchOnQuery。
最佳答案
我不是 Documentum 的专家,但我认为你需要一个更复杂的对象,你可以看看这个 repo https://github.com/ValentinBragaru/dfc-mock
IDfSessionMock我认为这就是您所需要的。
希望对您有所帮助。
关于java - 使用 Mockito 模拟 Documentum IDfSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25409362/