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

Version 1 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.

Using JMockit

Where MockArrays is the following Java class:

We use a Java class here because otherwise JMockit tries to replace other Groovy 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 on JMockit.

  • No labels