使用 Mockito 模拟 Java3D 的好方法是什么?

发布于 2024-10-18 13:15:58 字数 800 浏览 1 评论 0原文

我正在尝试模拟 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

打小就很酷 2024-10-25 13:15:58

恐怕你的问题没有答案。如果您想在测试期间避免意外的静态调用(它不依赖于您正在使用的模拟框架),您可以将您的调用包装到VirtualUniverse

例如:

public class VirtualUniverseWrapper {
  private VirtualUniverse virtualUniverse;
  // ...
  public Object foo() {
    // simple delegation instead of inheritance, because your class might be final
    return virtualUniverse.foo();
  }
}

如果您使用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:

public class VirtualUniverseWrapper {
  private VirtualUniverse virtualUniverse;
  // ...
  public Object foo() {
    // simple delegation instead of inheritance, because your class might be final
    return virtualUniverse.foo();
  }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文