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



根据PowerMock文档,我应该能够使用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.IollegalArgumentException:无法为最终类创建子类com.test.powermockstatics.FinalClassWithStaticCallorg.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:447)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.create.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:110)在org.mockito.internal.create.jmock.ClassImposterizer.oimposterise(ClassImposterizer.java:62)在org.powermock.api.mmockito.internal.mmockcreate.MockCreator.createMethodInvocationControl(MockCreator.java:111)在org.powermock.api.mokito.internal.mokcreation.MokCreator.mock(MockCreator.java:60)在org.powermock.api.mokito.PowerMockito.mokStatic(PowerMockito.java:70)在com.test.powermockstatics.TestStaticMockingWithoutPowerMockRunner.testStaticCall位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在java.lang.reflect.Method.ioke(Method.java:606)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.invokeExploly(FrameworkMethod.java:44)在org.junit.internal.runners.statements.InvokeMethod.eevaluate(InvokeMethod.java:17)在org.powermock.modules.junit4.rule.PowerMockStatement.eevaluate(PowerMockRule.java:49)网址:org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)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)org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)org.junit.runners.ParentRunner.access$000(ParentRunner.java:53),网址:org.junit.runners.ParentRunner$2.eevaluate(ParentRunner.java:229)org.junit.runners.ParentRunner.run(ParentRunner:309)org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TtestReference.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)

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

将powermock-module-unit4-rule-agent放在junit之前类路径

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

编辑:

请参阅Gábor Lipták回答下的评论中的澄清细节。我不想使用静态加载的代理,因为动态加载的代理似乎应该能够完成任务

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

您需要为测试准备类!

@PrepareForTest(MyFinalClass.class

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

根据文件:

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

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

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

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

public class NetworkManagerUtilsTest extends PowerMockTestCase {

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

相关内容

  • 没有找到相关文章

最新更新