[英]EasyMock and JNA - Mock Generic Return Type
I am trying to mock the following JNA call using EasyMock 我试图使用EasyMock模拟以下JNA调用
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 我使用JNA库的4.3.0版获得以下错误
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 对于JNA库4.2.0版,相同的代码也可以
Here are the method signatures for the method I am trying to mock 以下是我试图模拟的方法的方法签名
Version 4.2.0 版本4.2.0
public static Object loadLibrary(String name, Class interfaceClass) {
Version 4.3 版本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? 如何使用EasyMock模拟返回泛型返回类型?
Thanks Damien 谢谢Damien
The problem comes from your typing of Capture<Class<?>>
. 问题来自您输入
Capture<Class<?>>
。 This wildcard means you want to capture something
. 这个通配符意味着你想捕获
something
。 Let's call this something capture#1
. 让我们称之为
capture#1
。 So loadLibrary
is getting a class of capture#1
in parameter and to return a capture#1
instance. 因此
loadLibrary
在参数中获取一类capture#1
并返回capture#1
实例。 So andReturn
expects a capture#1
in parameter. 所以
andReturn
期望在参数中capture#1
。
But you are passing a ConvInterface
which isn't (obviously) a capture#1
. 但是你传递的是一个不是(显然)
capture#1
的ConvInterface
。
The solution is easy. 解决方案很简单。 Just do
Capture<Class<ConvInterface>> myClassCapture = EasyMock.newCapture();
只需执行
Capture<Class<ConvInterface>> myClassCapture = EasyMock.newCapture();
since this is what you expect loadLibrary
to get. 因为这是你期望
loadLibrary
得到的。
I believe the simplest answer is: don't use Capture
for this test, there is no need. 我相信最简单的答案是:不要使用
Capture
进行此测试,没有必要。
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: 这个问题使用了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.