IllegalStateException (due to ArrayIndexOutOfBoundsException) when running PowerMock test

2,036 views
Skip to first unread message

toddrun

unread,
Feb 17, 2011, 1:49:10 PM2/17/11
to PowerMock
I'm trying to run a test against a class that is stuck using a bunch
of statics and I've had success with powermock before - but not this
time. I'm pretty sure there's some dependency that's messed up, but I
don't even know where to start looking for a solution. Any help would
be appreciated!

The following is a dumbed down class and test, just to show that there
isn't anything out of the ordinary in my code:


public class DoTheThing {
private static final Logger logger =
LogManager.getLogger(DoTheThing.class);

public String doIt() {
logger.info("Fooing");
return Globals.getJiveProperty("foo", "bar");
}
}


@RunWith(PowerMockRunner.class)
@PrepareForTest({Appender.class} )
@SuppressStaticInitializationFor("com.doit.DoTheThing")
public class DoTheThingTest {

private DoTheThing thing;

@Mock private Logger mockLogger;

@Before
public void setup() {
thing = new DoTheThing();
Whitebox.setInternalState(DoTheThing.class, mockLogger);
PowerMockito.mockStatic(Globals.class);
}

@Test
public void doTest() {
when(Globals.getProperty("foo", "bar")).thenReturn("blah");

Assert.assertEquals("blah", thing.doIt());

PowerMockito.verifyStatic();
Globals.getProperty("foo", "bar");
verify(mockLogger).info("Fooing");
}

}

This results in:
java.lang.IllegalStateException: Failed to transform class with name
com.doit.DoTheThingTest. Reason: 3
at
org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:
208)
at
org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:
146)
at
org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:
65)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:
143)
at
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:
49)
at
org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:
231)
at
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:
69)
at
org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:
36)
at
org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:
27)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:
39)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:
27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at
org.junit.internal.requests.ClassRequest.buildRunner(ClassRequest.java:
33)
at
org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:
28)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:
32)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:
25)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:
41)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:
31)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
452)
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)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 3
at javassist.bytecode.ByteArray.write16bit(ByteArray.java:40)
at javassist.bytecode.StackMapTable$Shifter.update(StackMapTable.java:
744)
at javassist.bytecode.StackMapTable
$Shifter.sameFrame(StackMapTable.java:720)
at javassist.bytecode.StackMapTable
$Walker.stackMapFrames(StackMapTable.java:194)
at javassist.bytecode.StackMapTable$Walker.parse(StackMapTable.java:
179)
at javassist.bytecode.StackMapTable$Shifter.doit(StackMapTable.java:
714)
at javassist.bytecode.StackMapTable.shiftPc(StackMapTable.java:693)
at javassist.bytecode.CodeIterator.insertGap0(CodeIterator.java:676)
at javassist.bytecode.CodeIterator.insertGap(CodeIterator.java:636)
at javassist.bytecode.CodeIterator.insertGapCore(CodeIterator.java:
467)
at javassist.bytecode.CodeIterator.insertGap(CodeIterator.java:413)
at javassist.expr.Expr.replace0(Expr.java:298)
at javassist.expr.FieldAccess.replace(FieldAccess.java:213)
at org.powermock.core.transformers.impl.MainMockTransformer
$PowerMockExpressionEditor.edit(MainMockTransformer.java:233)
at javassist.expr.ExprEditor.loopBody(ExprEditor.java:197)
at javassist.expr.ExprEditor.doit(ExprEditor.java:90)
at javassist.CtClassType.instrument(CtClassType.java:1289)
at
org.powermock.core.transformers.impl.MainMockTransformer.transform(MainMockTransformer.java:
70)
at
org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:
204)
... 25 more

Here's a section of running mvn dependency:tree (hopefully I'm not
leaving out a part that's important - if I am, let me know what else I
should include!)
[INFO] +- junit:junit:jar:4.4:test
[INFO] +- org.objenesis:objenesis:jar:1.0:test
[INFO] +- cglib:cglib:jar:2.1:test
[INFO] | +- asm:asm:jar:1.5.3:test
[INFO] | +- asm:asm-util:jar:1.3.4:test
[INFO] | \- aspectwerkz:aspectwerkz-core:jar:0.8.1:test
[INFO] +- org.jmock:jmock-junit4:jar:2.4.0:test
[INFO] | +- org.jmock:jmock:jar:2.4.0:test
[INFO] | | +- org.hamcrest:hamcrest-core:jar:1.1:test
[INFO] | | \- org.hamcrest:hamcrest-library:jar:1.1:test
[INFO] | \- junit:junit-dep:jar:4.4:test
[INFO] +- org.powermock.modules:powermock-module-junit4:jar:1.4.5:test
[INFO] | \- org.powermock.modules:powermock-module-junit4-common:jar:
1.4.5:test
[INFO] | +- org.powermock:powermock-core:jar:1.4.5:test
[INFO] | | \- javassist:javassist:jar:3.13.0.GA:test
[INFO] | \- org.powermock.reflect:powermock-reflect:jar:1.4.5:test
[INFO] \- org.powermock.api:powermock-api-mockito:jar:1.4.5:test
[INFO] +- org.mockito:mockito-all:jar:1.8.5:test
[INFO] \- org.powermock.api:powermock-api-support:jar:1.4.5:test

And, just to be complete, here's the relevant part of my pom:

<repositories>
<repository>
<id>powermock-repo</id>
<url>http://powermock.googlecode.com/svn/repo/</url>
</repository>
</repositories>
.... more stuff ....
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-junit4</artifactId>
<version>2.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock.modules</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock.api</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
.... more stuff ....
<powermock.version>1.4.5</powermock.version>


Any help would be greatly appreciated! Happy to provide any additional
info to get this resolved (I can't imagine not being able to use
powermock - working with existing code will be unbearable - or at
least untestable - until I get this fixed).

Johan Haleby

unread,
Feb 18, 2011, 2:43:35 AM2/18/11
to powe...@googlegroups.com
Hi, 

It looks like problem with the byte-code manipulation. If you're lucky it could solve itself with the latest version of PowerMock (1.4.7) which uses a newer version of Javassist. So my first suggestion is that you should try upgrading and see how it goes.

/Johan


--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
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.


toddrun

unread,
Feb 18, 2011, 11:27:52 AM2/18/11
to PowerMock
It was indeed a problem with Javassist, but it was due to another
dependency pulling in an older version. I had suspected this and tried
to put an exclusion in, but until a colleague pointed out that I had
the wrong groupId, the old version of javassist keep trumping the one
provided by powermock.

Thanks for the help!

On Feb 17, 11:43 pm, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hi,
>
> It looks like problem with the byte-code manipulation. If you're lucky it
> could solve itself with the latest version of PowerMock (1.4.7) which uses a
> newer version of Javassist. So my first suggestion is that you should try
> upgrading and see how it goes.
>
> /Johan
>

Denis Kozlov

unread,
Jun 26, 2014, 5:47:35 AM6/26/14
to powe...@googlegroups.com, tod...@gmail.com
Thanks toddrun if it wasn't you I would spend some more hours figuring this out! Because only groupId was different javassist (old) and org.javassist (new).

пятница, 18 февраля 2011 г., 20:27:52 UTC+4 пользователь toddrun написал:
Reply all
Reply to author
Forward
0 new messages