Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Suppose you wish to test a class which is dependent on a static call. Is there a way to mock out that call?

Yes, there are two suggested approaches: Using EMC or Using JMockit

Using EMC

Here we are calling Arrays.sort() directly - normally that would be the problematic code within your class under test.

More details about this approach: ExpandoMetaClass - Adding static methods

Using JMockit

Where MockArrays is the following Java class:

We use a Java class here because otherwise JMockit tries to replace other GroovyObject methods (e.g. getMetaClass, invokeMethod, ...) and won't find them inside the java.util.Arrays class. Obviously, if your redefining a Groovy class, you can use another Groovy class.

More details: Using JMockit with Groovy.

  • No labels