Note that there are some explanatory texts on larger screens.

plurals
  1. POPowerMock - NPE on @PrepareForTest
    primarykey
    data
    text
    <p>Problem: The instrumentation class in PowerMock isn't being initialized. I'm loading the library through the @Rule annotation. I've got the VM argument in place: -javaagent:project_path/libs/powermock-module-javaagent-1.5.jar</p> <p>When I walk through with the debugger, PowerMockAgent#premain gets called and the instrumentation object gets set, but when it's called during the test setup, the instrumentation object is null, throwing the error below. </p> <p>Why is the instrumentation variable not keeping it's value between when PowerMockAgent#premain is called and when PowerMockClassRedefiner.redefine calls it?</p> <p><strong>Instrumentation gets set correctly here on load:</strong></p> <pre><code> private static void initialize(String agentArgs, Instrumentation inst) throws IOException { instrumentation = inst; inst.addTransformer(new DefinalizingClassTransformer(), false); inst.addTransformer(classTransformer, true); } </code></pre> <p><strong>But when called again from @PrepareForTest({ Logger.class }), is null</strong></p> <pre><code>public class PowerMockClassRedefiner { public static void redefine(Class&lt;?&gt; cls) { if(cls == null) { throw new IllegalArgumentException("Class to redefine cannot be null"); } PowerMockAgent.getClasstransformer().setClassesToTransform(Arrays.asList(cls.getName())); try { PowerMockAgent.instrumentation().retransformClasses(cls); } catch(Exception e){ throw new RuntimeException("Failed to redefine class "+cls.getName(), e); } } </code></pre> <p>Resulting in error: </p> <pre><code>java.lang.RuntimeException: Failed to redefine class com.testapp.Logger at org.powermock.modules.agent.PowerMockClassRedefiner.redefine(PowerMockClassRedefiner.java:33) at org.powermock.modules.agent.PowerMockClassRedefiner.redefine(PowerMockClassRedefiner.java:42) at com.testapp.testFramework.PowerMockRuleAgentSetup.redefine(PowerMockRuleAgentSetup.java:29) at com.testapp.testFramework.PowerMockRuleAgentSetup.initialize(PowerMockRuleAgentSetup.java:19) at com.testapp.testFramework.PowerMockOverriderRule.apply(PowerMockOverriderRule.java:19) at org.junit.runners.BlockJUnit4ClassRunner.withMethodRules(BlockJUnit4ClassRunner.java:341) at org.junit.runners.BlockJUnit4ClassRunner.withRules(BlockJUnit4ClassRunner.java:330) at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:248) at com.xtremelabs.robolectric.RobolectricTestRunner.methodBlock(RobolectricTestRunner.java:287) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 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) Caused by: java.lang.NullPointerException at org.powermock.modules.agent.PowerMockClassRedefiner.redefine(PowerMockClassRedefiner.java:31) ... 22 more </code></pre> <p>Here's my test setup:</p> <pre><code>@RunWith(RobolectricTestRunner.class) @PrepareForTest({ Logger.class }) // THIS IS WHERE THE ERROR GETS THROWN public class LoggerTest { @Rule public PowerMockOverriderRule rule = new PowerMockOverriderRule(); public LoggerConfig config; @Before public void setUp() { config = mock(LoggerConfig.class); } </code></pre> <p>UPDATE: Removing the @RunWith(RobolectricTestRunner.class) fixes the problem, but isn't an option for my project. (It's an Android project).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload