Reputation: 1292
I write my unit tests for my Java application with Groovy, JUnit and EasyMock.
In EasyMock there are several overloaded methods capture()
which have been deprecated with the note "Because of harder erasure enforcement, doesn't compile in Java 7". The methods take as parameter an object of type Capture<T>
. There exist, among others, the following methods:
static boolean capture(Capture<Boolean> captured)
static boolean capture(Capture<Integer> captured)
static <T> T capture(Capture<T> captured)
This is not allowed any more in Java but if you invoke that code directly from Java the right method gets invoked. E.g. when you execute this code
Capture<MyClass> myClassCapture = new Capture<MyClass>();
mockObject.someMethod(capture(myClassCapture));
The right method (the last in the list) gets invoked.
On the other hand, if you invoke the same code from inside Groovy, the first method in the list is invoked and gives an error in my test. I think that this has do to with how Java and Groovy resolve the methods. My assumption is that Java binds the method at compile time while Groovy tries to find the method at runtime and takes any method it can find (maybe the first).
Can anybody explain exactly what happens here? This would be nice to understand the different behaviour between Java and Groovy more precisely.
I fixed it by delegating the call inside Groovy to a Java method which will do the job for me:
public class EasyMockUtils {
public static <T> T captureObject(Capture<T> captureForObject) {
return EasyMock.capture(captureForObject);
}
}
Is there maybe a better way?
Upvotes: 4
Views: 1103
Reputation: 774
Just hit this issue myself using EasyMock 3.0. However it looks like it's been resolved as of EasyMock 3.2 by renaming all the methods that took wrapped primitives and leaving just one remaining capture method.
Check the 3.2 docs for more info: http://easymock.org/api/easymock/3.2/org/easymock/EasyMock.html#capture%28org.easymock.Capture%29
Upvotes: 1
Reputation: 4444
Try to use @CompileStatic of Groovy 2.0 - may be it will resolve your problem
Upvotes: 0