Damien :
I am trying to mock the following JNA call using EasyMock
convInterface = (ConvInterface) Native.loadLibrary(libraryLocation,ConvInterface.class);
Using this test method
@Test
public void testLib() {
Capture<Class<?>> myClassCapture = EasyMock.newCapture();
PowerMock.mockStatic(Native.class);
EasyMock.expect(Native.loadLibrary(EasyMock.isA(String.class), EasyMock.capture(myClassCapture))).andReturn(mockConvInterface);
PowerMock.replay(Native.class);
ConvServiceImpl myLib = new ConvServiceImpl();
myLib.instantiateConvLibrary();
PowerMock.verify(Native.class);
}
I am getting the following error using version 4.3.0 of the JNA library
The method andReturn(capture#1-of ?) in the type IExpectationSetters<capture#1-of ?> is not applicable for the arguments (ConvInterface)
The same code is fine with version 4.2.0 of the JNA library
Here are the method signatures for the method I am trying to mock
Version 4.2.0
public static Object loadLibrary(String name, Class interfaceClass) {
Version 4.3
public static <T> T loadLibrary(String name, Class<T> interfaceClass) {
How do I go about mocking the return of a generic return type using EasyMock?
Thanks Damien
Rogério :
I believe the simplest answer is: don't use Capture
for this test, there is no need.
The question uses PowerMock + EasyMock, but since I don't have these libraries on hand, here is a much shorter version of the test (verified to work), using JMockit:
import org.junit.*;
import static org.junit.Assert.*;
import mockit.*;
import com.sun.jna.*;
public final class JNATest {
public interface ConvInterface extends Library {}
public static class ConvServiceImpl {
final String libraryLocation = "whatever";
ConvInterface convInterface;
public void instantiateConvLibrary() {
convInterface = Native.loadLibrary(libraryLocation, ConvInterface.class);
}
}
@Test
public void testLib(
@Mocked Native mockNative, @Mocked ConvInterface mockConvInterface
) {
ConvServiceImpl myLib = new ConvServiceImpl();
myLib.instantiateConvLibrary();
assertSame(mockConvInterface, myLib.convInterface);
}
}
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=469287&siteId=1