是否可以使用PowerMockRule而不是PowerMockRunner在最终类上模拟静态方法?

时间:2014-09-15 14:42:44

标签: junit mockito powermock junit-runner junit-rule

根据the PowerMock docs,我应该可以使用PowerMockRule代替@RunWith(PowerMockRunner.class)来运行,并获得相同的结果。

我似乎找到了一个不存在这种情况的案例。

以下示例运行良好:

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

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

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

我得到以下异常:

  

java.lang.IllegalArgumentException:不能继承最终类类   com.test.powermockstatics.FinalClassWithStaticCall at   org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:447)at at   org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)     在   org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)     在org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378)     在org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318)     在   org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:110)     在   org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:62)     在   org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:111)     在   org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:60)     在   org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:70)     在   com.test.powermockstatics.TestStaticMockingWithoutPowerMockRunner.testStaticCall(TestStaticMockingWithoutPowerMockRunner.java:30)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:606)at   org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47)     在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)     在   org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)     在   org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)     在   org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)     在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)at   org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)     在   org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)at at   org.junit.runners.ParentRunner.run(ParentRunner.java:309)at at   org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)     在   org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我遵循文档的建议:

  

在junit之前放置powermock-module-junit4-rule-agent   类路径

如果这是PowerMock中的错误或所需的行为(即,您根本无法使用{{1}在static类上模拟final方法,是否有人知道这个官方字词? })?

修改

请参阅the comments under Gábor Lipták's answer中的澄清细节。我想要使用静态加载的代理,因为看起来动态加载的代理应该能够完成工作吗?

我知道静态启动代理会起作用。 (不幸的是,这不是我项目中的一个选项。)那么有人知道动态加载的Agent的失败是否是PowerMock中的错误?或者一个已知的限制;为什么?

3 个答案:

答案 0 :(得分:15)

您需要准备课程以进行测试!

@PrepareForTest(MyFinalClass.class

答案 1 :(得分:1)

对于模拟最终类,classpath是不够的。您需要JVM代理。

根据docs

  

在某些情况下(例如模拟最终类)可能有必要   在Maven中热切地加载PowerMock代理以进行测试   在Surefire工作。如果您遇到这种情况,请添加以下内容   你的pom.xml:

模拟最终类所需的JVM参数:

-javaagent:${settings.localRepository}/org/powermock/powermock-module-javaagent/1.5.6/powermock-module-javaagent-1.5.6.jar

答案 2 :(得分:1)

我有相同的症状,可以通过从PowerMockTestCase扩展测试类来解决它。

public class NetworkManagerUtilsTest extends PowerMockTestCase {

不确定此解决方案是否适用于此。