gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:52:30 26 4
gpt4 key购买 nike

根据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: Cannot subclass final class class com.test.powermockstatics.FinalClassWithStaticCall at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:447) at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217) at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378) at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318) at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:110) at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:62) at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:111) at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:60) at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:70) at com.test.powermockstatics.TestStaticMockingWithoutPowerMockRunner.testStaticCall(TestStaticMockingWithoutPowerMockRunner.java:30) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at 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 org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 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)

我遵循文档的建议:

put powermock-module-junit4-rule-agent before junit in the classpath

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

编辑:

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

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

最佳答案

您需要为类(class)做好测试准备!

@PrepareForTest(MyFinalClass.class)

关于junit - 是否可以使用 PowerMockRule 而不是 PowerMockRunner 在最终类上模拟静态方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25850673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com