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.
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.
精彩评论