简体   繁体   中英

EasyMock and JNA - Mock Generic Return Type

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

The problem comes from your typing of Capture<Class<?>> . This wildcard means you want to capture something . Let's call this something capture#1 . So loadLibrary is getting a class of capture#1 in parameter and to return a capture#1 instance. So andReturn expects a capture#1 in parameter.

But you are passing a ConvInterface which isn't (obviously) a capture#1 .

The solution is easy. Just do Capture<Class<ConvInterface>> myClassCapture = EasyMock.newCapture(); since this is what you expect loadLibrary to get.

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM