gpt4 book ai didi

junit - PowerMockito 在模拟枚举的静态方法并在 switch 语句中使用该枚举对象时抛出异常

转载 作者:行者123 更新时间:2023-12-02 04:41:16 27 4
gpt4 key购买 nike

我有一个 enum MyEnum.java,它有一个 static 方法。

//MyEnum.java

enum MyEnum {

ONE("one"),
TWO("two");

private String value;
MyEnum(String value){
this.value=value;
}

public static MyEnum getMyEnum(String value){
for(MyEnum myEnum : MyEnum.values()){
if(myEnum.value.equalsIgnoreCase(value))
return myEnum;
}
return null;
}
}

我正在使用 PowerMock 来模拟此 enumstatic 方法。我已经包含了所有必要的条件,例如

@Runwith(PowerMockRunner.class)
@PrepareForTest(MyEnum.class)

//Test function
PowerMockito.mockStatic(MyEnum.class);

一切正常。但是如果我对枚举对象使用 switch 方法,那么它会抛出异常。

MyEnum enum = MyEnum.ONE;
switch(enum){
case ONE:
break
}

此代码抛出以下异常。

    java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:191)
at javassist.runtime.Desc.getClassObject(Desc.java:43)
at javassist.runtime.Desc.getClazz(Desc.java:52)
at com.newshunt.shared.presenters.tests.DummyTest.testStaticMethod(DummyTest.java:26)
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.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.NullPointerException
at com.newshunt.shared.presenters.tests.DummyTest$1.<clinit>(DummyTest.java:26)
... 37 more


Process finished with exit code -1

即使从日志中也不清楚,是什么导致了这个异常发生?以前有其他人遇到过类似问题吗?

最佳答案

我的建议:考虑使用 PowerMock。

PowerMock 看起来像是许多问题的解决方案;但迟早会导致更多问题。它破坏了覆盖范围,使更改底层 JVM 变得更加困难,等等。

说真的:如果您的设计只能使用 PowerMock 进行测试,这通常清楚地表明您的设计不好。所以:专注于重新编写测试中的代码!

在您的情况下:我会开始质疑在枚举上使用 static 方法的必要性。所以,换句话说:你想用这段代码解决什么问题?

但要回答实际问题:您必须了解枚举常量本身表示为内部类。因此,您必须为@PrepareForTest 使用fullyQualifiedName 属性。看到这个较新的 question举个例子。

关于junit - PowerMockito 在模拟枚举的静态方法并在 switch 语句中使用该枚举对象时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37297274/

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