3

问题:PowerMock 中的检测类没有被初始化。我正在通过 @Rule 注释加载库。我已经有了 VM 参数:-javaagent:project_path/libs/powermock-module-javaagent-1.5.jar

当我使用调试器进行操作时,会调用 PowerMockAgent#premain 并设置检测对象,但是在测试设置期间调用它时,检测对象为空,从而引发以下错误。

为什么检测变量在调用 PowerMockAgent#premain 和调用 PowerMockClassRedefiner.redefine 之间不保持其值?

加载时仪器在此处正确设置:

  private static void initialize(String agentArgs, Instrumentation inst) throws IOException {
    instrumentation = inst;
    inst.addTransformer(new DefinalizingClassTransformer(), false);
    inst.addTransformer(classTransformer, true);
}

但是当从@PrepareForTest({ Logger.class }) 再次调用时,为空

public class PowerMockClassRedefiner {

public static void redefine(Class<?> 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);
    }
}

导致错误:

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

这是我的测试设置:

@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);
}

更新:删除 @RunWith(RobolectricTestRunner.class) 解决了这个问题,但不是我的项目的选项。(这是一个Android项目)。

4

1 回答 1

0

我很抱歉地问,但你为什么还要模拟一个 Logger 类?如果要确保将输出记录到控制台,可以将记录器包装在可以模拟的抽象中。

class LogWraper {
   void someMethod() {
     logger.log()
   }
}

然后你可以创建一个你的 FooWraper 的模拟。根本不需要使用 Powermock ......

于 2016-01-29T22:03:37.463 回答