Org.powermock.reflect.exceptions.FieldNotFoundException when using PowerMock.expectNew

I wanted to make fun of the following code with 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); 

However, when I make fun of QueueingConsumer as 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(); } } 

I always get the following exception:

 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) 

I just don’t understand why. Is there something wrong with my code? I rewrote it using different approaches, but I will always come up with this error when using expectNew. Or is this the current issue for PowerMock 1.5.1?

-Thanks -

+4
source share
2 answers

What version of Easymock are you using? I have the same problem when I had version 2.0 of EasyMock, but using 3.1 it worked fine. I believe that Powermock 1.5.1 expects EasyMock 3.1 to be provided anyway, so this will be the correct version to use.

+3
source

I have the same error when using the following jars:

Seasonings

  • 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

Error

 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) 

I fixed this error by replacing easymock-3.2.jar with easymock-3.1.jar. Found on the official download site: http://easymock.org/Downloads.html

0
source

Source: https://habr.com/ru/post/1492714/


All Articles