vijayjcp / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 2 forks source link

@PrepareForTest fails for multi-dimensional array #392

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

1. Create a Java class "Example" containing a multi-dimensional array field, 
and an direct assignment of a "new" Object to one of the array elements (see 
below)
2. Create a JUnit test class with @RunWith(PowerMockRunner.class), 
@PrepareForTest(Example.class) and an empty @Test method
3. Run the JUnit test

What is the expected output? What do you see instead?

The (empty) test should succeed, but fails with an Exception (see attached 
file) thrown by javassist.

What version of the product are you using? On what operating system?

- powermock 1.4.12 (core, module-junit4, api-mockito)
- Apache Maven 3.0.4
- Windows XP Professional SP3

Please provide any additional information below.

Workaround 1: Use an intermediate local variable to store the new instance 
before assigning it to the array element

Workaround 2: Downgrade to javassist 3.15.0-GA

Here's the source code for the two classes involved:

---

public class Example {

    private Object[][] array = new Object[1][1];

    public void someMethod() {
        array[0][0] = new Object();
    }
}

---

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(Example.class)
public class ExampleTest {

    @Test
    public void testDummy() {
    }
}

Original issue reported on code.google.com by chs...@googlemail.com on 12 Jun 2012 at 9:00

Attachments:

GoogleCodeExporter commented 8 years ago
The error also occurs, if the RHS of the assignment is a method invocation. The 
same workarounds can be used in this case.

Original comment by chs...@googlemail.com on 12 Jun 2012 at 12:18