4

我想使用 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 版的当前问题?

-谢谢-

4

2 回答 2

3

您使用的是什么版本的 Easymock?当我有 3.2 版本的 EasyMock 时,我遇到了同样的问题,但使用 3.1 可以正常工作。我相信 Powermock 1.5.1 预计无论如何都会提供 EasyMock 3.1,所以这将是正确的版本。

于 2013-07-29T12:05:30.563 回答
0

使用以下罐子时,我遇到了同样的错误:

罐子

  • activemq-all-5.9.0.jar
  • com.ibm.mq.headers-7.0.1.5.jar
  • com.ibm.mq.jmqi-7.5.0.0.jar
  • com.ibm.mqjms-7.5.0.0.jar
  • javassist-3.9.0.GA.jar
  • junit-4.10.jar
  • powermock-easymock-1.5.1-full.jar
  • com.ibm.mq-7.0.1.5.jar
  • easymock-3.2.jar
  • objenesis-2.1.jar
  • cglib-nodep-2.2.jar
  • easymockclassextension-2.4.jar
  • com.ibm.disthub2-1.jar

错误

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)
    at QueueService.createQueueConnection(QueueService.java:207)
    at QueueService.sendMessage(QueueService.java:93)
    at QueueServiceTest.testSendMessage(QueueServiceTest.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:101)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我通过用 easymock-3.1.jar 替换 easymock-3.2.jar 修复了这个错误。在官方下载站点找到:http ://easymock.org/Downloads.html

于 2013-12-05T13:05:56.230 回答