Re: [powermock] TooManyFieldsFoundException instead of MockitoAssertionError when using verifyStatic

2,026 views
Skip to first unread message

Johan Haleby

unread,
Sep 18, 2012, 11:19:34 AM9/18/12
to powe...@googlegroups.com
You may be using an unsupported combination of Mockito and PowerMock. Have a look in the matrix here to see which combinations that are valid.

/Johan 

On Tue, Sep 18, 2012 at 4:36 PM, Philippe Prive <philipp...@gmail.com> wrote:
Here is my test:

package cn.ca.spike.station;

import static org.mockito.Mockito.never;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(StationCacheStatic.class)
public class StationServiceTestPower2 {

@Test
public void testStatic() {
String station333 = "abc";
String stationState = "def";
mockStatic(StationCacheStatic.class);
StationCacheStatic.getStation(station333, stationState);
verifyStatic(never());
StationCacheStatic.getStation(station333, stationState);
}
}

When I run it, I got the following:

org.powermock.reflect.exceptions.TooManyFieldsFoundException: Two or more fields matching type java.lang.String.
at org.powermock.reflect.internal.WhiteboxImpl.findSingleFieldUsingStrategy(WhiteboxImpl.java:496)
at org.powermock.reflect.internal.WhiteboxImpl.findFieldInHierarchy(WhiteboxImpl.java:455)
at org.powermock.reflect.internal.WhiteboxImpl.setInternalState(WhiteboxImpl.java:327)
at org.powermock.reflect.Whitebox.setInternalState(Whitebox.java:207)
at org.powermock.api.mockito.internal.invocationcontrol.InvocationControlAssertionError.updateErrorMessageForMethodInvocation(InvocationControlAssertionError.java:75)
at org.powermock.api.mockito.internal.invocationcontrol.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:300)
at org.powermock.api.mockito.internal.invocationcontrol.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:194)
at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:105)
at org.powermock.core.MockGateway.methodCall(MockGateway.java:60)
at cn.ca.spike.station.StationCacheStatic.getStation(StationCacheStatic.java)
at cn.ca.spike.station.StationServiceTestPower2.testStatic(StationServiceTestPower2.java:81)
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:312)
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:296)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:112)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:73)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:284)
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:209)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:148)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
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:120)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:102)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:42)
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)

What am I doing wrong?

Thank you

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To view this discussion on the web visit https://groups.google.com/d/msg/powermock/-/RGeKm83L2YkJ.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.

Matt Lachman

unread,
Sep 18, 2012, 11:19:33 AM9/18/12
to powe...@googlegroups.com
What method are you trying to test here? If you're trying to test "StationCacheStatic.getStation(...)" then you shouldn't mock it; you should mock the methods it calls. If you're trying to test some other method that calls getStation, well, I don't see it.

Matt

Philippe Prive

unread,
Sep 18, 2012, 11:36:14 AM9/18/12
to powe...@googlegroups.com
Matt, thank you for your explanation, but I know that this test is not relevant, I just wanted to isolate the usage of verifyStatic, since I was not able to have it to work correctly in my real test :)

Philippe Prive

unread,
Oct 29, 2012, 11:30:50 PM10/29/12
to powe...@googlegroups.com
I got some news...

First, I try to make my tests run via Eclipse 3.6... and it seems to be an Eclipse issue (but if you can help me I will really appreciate it)

I am now able to run the test in a particular way: only if I run my test method directly (right-click on the test method and run as Junit test). If I do the same but on the class, then I got the TooManyFieldsFoundException.

So it seems to be a classpath resolution that is different depending on the way I start Junit in Eclipse.

Any idea?

Kengo TODA

unread,
Sep 5, 2013, 12:54:38 AM9/5/13
to powe...@googlegroups.com
Hi ALL,

Today I faced this exception when I used IBM JDK which is distributed with IBM WebSphere.
And this problem was solved when I switched to Oracle JDK.

I did some tracing, and I think the reason is difference between fields of the Throwable class.
In IBM JDK, Throwable class has 2 fields (detailMessage and walkback) which matches to FieldTypeMatcherStrategy so PowerMockito throws exception.


I wish this information helps you to solve this problem.

Johan Haleby

unread,
Sep 5, 2013, 1:10:10 AM9/5/13
to powe...@googlegroups.com
Interesting! This is truly hard for me to try out since I don't have an IBM JDK. Please help out by providing a patch for this if you like.


To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.

To post to this group, send email to powe...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages