以使用PowerMockRule而不是PowerMockRunn

以使用PowerMockRule而不是PowerMockRunn

本文介绍了是否可以使用PowerMockRule而不是PowerMockRunner在最终类上模拟静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 PowerMock文档,我应该可以使用PowerMockRule而不是@RunWith(PowerMockRunner.class)并获得相同的结果.

According to the PowerMock docs, I should be able to run using a PowerMockRule instead of @RunWith(PowerMockRunner.class) and get the same results.

我似乎发现情况并非如此.

I seem to have found a case where this isn't true.

以下示例运行正常:

package com.test.powermockstatics;

import static org.junit.Assert.assertEquals;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

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

final class FinalClassWithStaticCall {
  public static int getIntStatic() {
    return 1;
  }
}

@RunWith(PowerMockRunner.class)
@PrepareForTest(FinalClassWithStaticCall.class)
public class TestStaticMockingWithoutPowerMockRunner {
  @Test
  public void testStaticCall() {
    mockStatic(FinalClassWithStaticCall.class);
    when(FinalClassWithStaticCall.getIntStatic()).thenReturn(2);

    assertEquals(FinalClassWithStaticCall.getIntStatic(), 2);
  }
}

但是当切换到这样的规则时:

But when switched to a rule like so:

package com.test.powermockstatics;

import static org.junit.Assert.assertEquals;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

import org.junit.Rule;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.agent.PowerMockAgent;
import org.powermock.modules.junit4.rule.PowerMockRule;

final class FinalClassWithStaticCall {
  public static int getIntStatic() {
    return 1;
  }
}

@PrepareForTest(FinalClassWithStaticCall.class)
public class TestStaticMockingWithoutPowerMockRunner {
  static {
    PowerMockAgent.initializeIfNeeded();
  }

  @Rule
  public PowerMockRule rule = new PowerMockRule();

  @Test
  public void testStaticCall() {
    mockStatic(FinalClassWithStaticCall.class);
    when(FinalClassWithStaticCall.getIntStatic()).thenReturn(2);

    assertEquals(FinalClassWithStaticCall.getIntStatic(), 2);
  }
}

我收到以下异常:

我正在遵循文档中的建议:

I am following the recommendation from the docs to:

如果这是PowerMock中的错误或所需的行为(例如,您根本无法使用PowerMockRulefinal类上模拟static方法),有人知道吗?

Does anyone know the official word if this is a bug in PowerMock or the desired behavior (i.e., you simply can't mock a static method on a final class using a PowerMockRule)?

请参阅GáborLipták的回答下的评论.我不是要使用静态加载的代理,因为看起来动态加载的代理应该能够完成工作?

Please see the clarifying details in the comments under Gábor Lipták's answer. I do not want to use a statically loaded Agent, since it appears the dynamically loaded Agent ought to be capable of getting the job done?

我知道以静态方式启动代理会起作用. (不幸的是,这不是我的项目中的选项.)那么,有人知道动态加载的代理的故障是否是PowerMock中的错误吗?或已知的限制;为什么?

I know starting the agent statically will work. (Unfortunately this is not an option in my project.) So does anyone know if the failure of the dynamically loaded Agent is a bug in PowerMock? Or a known limitation; and why?

推荐答案

您需要准备要测试的课程!

You need to prepare the class for test!

@PrepareForTest(MyFinalClass.class)

这篇关于是否可以使用PowerMockRule而不是PowerMockRunner在最终类上模拟静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 01:32