使用 Mockito 模拟 Java3D 的好方法是什么?
我正在尝试模拟 Java3D 类以进行单元测试,例如:
mock(VirtualUniverse.class);
或者
mock(Canvas3D.class);
不幸的是,VirtualUniverse(也由 Canvas3D 引用)有一个对 MasterControl 的静态引用,其中包括一个
static void loadLibraries(){
...
}
在模拟期间调用并尝试加载外部库的方法,这正是我想要避免的。
我想听听人们使用什么作为模拟应用于使用 Java3D 框架的应用程序的通用方法,特别是如果您找到了一种令人满意的处理宇宙的方法。
更新:
问这个问题后发生了一些事情。一是我们更多地了解了 Java3D 和 JavaFX 的现状。 Java3D 的工作似乎暂时停止,转而专注于 JavaFX。此外,JavaFX 预计将于 2011 年第 3 季度包含 Java API。由于我们现有的代码是基于场景图的,所以我四处寻找其他场景图范例工具,并偶然发现了 jMonkeyEngine (jME),它似乎对我们来说很有效。
虽然 jME 的应用程序类更喜欢继承而不是组合(请参阅 com.jme3.app.SimpleApplication),但将委托者插入到继承层次结构中很容易,这使我能够以更支持 TDD 的方式创建我们自己的应用程序。此外,jME 团队一直擅长避免使用静态行为,这再次有助于模拟 UT 组件。
因此,我接受 Zsolt 的回答,因为他与代表团的想法是一致的。
I'm trying to mock out Java3D classes for unit tests, for example:
mock(VirtualUniverse.class);
or
mock(Canvas3D.class);
Unfortunately, VirtualUniverse (which is also referenced by Canvas3D) has a static reference to MasterControl which includes a method
static void loadLibraries(){
...
}
which is invoked during mocking and tries to load external libs, which is exactly what I'm trying to avoid.
I'd like to hear what people have used as a general approach to mocking applied to applications that make use of the Java3D framework, especially if you found a satisfactory approach to dealing with Universes.
Update:
A couple things happened after asking this question. One is that we learned more about the state of Java3D and JavaFX. It seems that work on Java3D is being stopped for now in favor of focusing on JavaFX. Also, JavaFX is slated to include a Java API in Q3 2011 at this time. Since our existing code is scenegraph-based, I looked around for other scenegraph paradigm tools, and stumbled across jMonkeyEngine (jME), which seem like it will work well for us.
While jME's application class prefers inheritance over composition (see com.jme3.app.SimpleApplication), it was easy enough to insert a delegator into the inheritance hierarchy, allowing me to create our own application in a more TDD-able fashion. Also, the jME team has been good about avoiding use of static behaviors, which again helps in the effort to mock out components for UTs.
So, I'm accepting Zsolt's answer on the basis that he's on the money with the delegation thought.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恐怕你的问题没有答案。如果您想在测试期间避免意外的静态调用(它不依赖于您正在使用的模拟框架),您可以将您的调用包装到
VirtualUniverse
。例如:
如果您使用
VirtualUniverseWrapper
,您可以模拟它。这可能会让你的代码有点奇怪,但它确实有效。我们在代码库中使用相同的方法将包装器与工厂结合起来。I'm afraid there is no answer for your question. If you want to avoid unexpected static calls during your testing - it does not depend on the mocking framework you are using -, you may wrap your calls towards the
VirtualUniverse
.For example:
If you use the
VirtualUniverseWrapper
, you can mock it. It might make your code a bit strange, but it works. We are using the same approach in our code base combining the wrappers with factories.