繁体   English   中英

EasyMock和JNA - 模拟通用返回类型

[英]EasyMock and JNA - Mock Generic Return Type

我试图使用EasyMock模拟以下JNA调用

convInterface = (ConvInterface)  Native.loadLibrary(libraryLocation,ConvInterface.class);

使用这种测试方法

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

我使用JNA库的4.3.0版获得以下错误

The method andReturn(capture#1-of ?) in the type IExpectationSetters<capture#1-of ?> is not applicable for the arguments (ConvInterface)

对于JNA库4.2.0版,相同的代码也可以

以下是我试图模拟的方法的方法签名

版本4.2.0

public static Object loadLibrary(String name, Class interfaceClass) {

版本4.3

public static <T> T loadLibrary(String name, Class<T> interfaceClass) {

如何使用EasyMock模拟返回泛型返回类型?

谢谢Damien

问题来自您输入Capture<Class<?>> 这个通配符意味着你想捕获something 让我们称之为capture#1 因此loadLibrary在参数中获取一类capture#1并返回capture#1实例。 所以andReturn期望在参数中capture#1

但是你传递的是一个不是(显然) capture#1ConvInterface

解决方案很简单。 只需执行Capture<Class<ConvInterface>> myClassCapture = EasyMock.newCapture(); 因为这是你期望loadLibrary得到的。

我相信最简单的答案是:不要使用Capture进行此测试,没有必要。

这个问题使用了PowerMock + EasyMock,但由于我手头没有这些库,所以使用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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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