我想使用 PowerMock 模拟以下代码:
ConnectionFactory rabbitMqFactory = createFactory();
com.rabbitmq.client.Connection connection = rabbitMqFactory.newConnection();
com.rabbitmq.client.Channel channel = con.createChannel();
com.rabbitmq.client.QueueingConsumer consumer = new com.rabbitmq.client.QueueingConsumer(channel);
但是,当我将 QueueingConsumer 模拟为 expectNew 时:
@RunWith(PowerMockRunner.class)
@PrepareForTest({MyClass.class})
public class MyClassTester {
@Test
public void test() {
QueueingConsumer consumer = createMock(QueueingConsumer.class);
MyClass myClass = new MyClass();
// other code mock here ...
PowerMock.expectNew(QueueingConsumer.class, Channel.class).andReturn(consumer);
replayAll();
myClass.callRabbitMq();
verifyAll();
}
}
我总是得到以下异常:
org.powermock.reflect.exceptions.FieldNotFoundException: No instance field of type "org.easymock.internal.MocksControl$MockType" could be found in the class hierarchy of org.easymock.internal.MocksControl.
at org.powermock.reflect.internal.matcherstrategies.FieldTypeMatcherStrategy.notFound(FieldTypeMatcherStrategy.java:40)
at org.powermock.reflect.internal.WhiteboxImpl.findSingleFieldUsingStrategy(WhiteboxImpl.java:509)
at org.powermock.reflect.internal.WhiteboxImpl.findFieldInHierarchy(WhiteboxImpl.java:455)
at org.powermock.reflect.internal.WhiteboxImpl.getInternalState(WhiteboxImpl.java:578)
at org.powermock.api.easymock.internal.invocationcontrol.EasyMockMethodInvocationControl.getMockType(EasyMockMethodInvocationControl.java:99)
at org.powermock.api.easymock.internal.invocationcontrol.NewInvocationControlImpl.invoke(NewInvocationControlImpl.java:53)
at org.powermock.core.MockGateway.newInstanceCall(MockGateway.java:191)
我只是不明白为什么。我的代码有问题吗?我已经用不同的方法重写了它,但是在使用 expectNew 时总是会出现这个错误。或者这是 PowerMock 1.5.1 版的当前问题?
-谢谢-