【问题标题】:java.lang.ExceptionInInitializerError - PowerMockitojava.lang.ExceptionInInitializerError - PowerMockito
【发布时间】:2017-08-15 05:16:30
【问题描述】:

所以我尝试使用 PowerMockito 代替 Mockito,这样我就可以模拟一个最终的类。但是,在我切换到 PowerMockito 后,在尝试模拟任何类时都会得到以下 stackTrace:

java.lang.ExceptionInInitializerError
at org.powermock.api.mockito.repackaged.ClassImposterizer.createProxyClass(ClassImposterizer.java:95)
at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:57)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:111)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:59)
at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:143)
at com.app.SettingsTests.setup(SettingsTests.java:64)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)
Caused by: org.mockito.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:238)
at org.mockito.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:117)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:109)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:105)
at org.mockito.cglib.proxy.Enhancer.<clinit>(Enhancer.java:70)
... 33 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at org.mockito.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:385)
at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:220)
... 38 more
Caused by: java.lang.UnsupportedOperationException: can't load this type of class file
at java.lang.ClassLoader.defineClass(ClassLoader.java:594)
... 41 more

这里是设置方法:

@RunWith(AndroidJUnit4.class)
@PrepareForTest(SettingsView.class)
public class SettingsTests {

  @Mock private SettingsView view;

  @Before
  public void setup() {
    view = mock(SettingsView.class);
  }

有谁知道可能导致这种情况的原因是什么?我试图将mock(SettingsView.class) 更改为PowerMockito.mock(SettingsView.class),但我得到了相同的结果。

另外,视图是这个场景中的一个界面。

【问题讨论】:

    标签: java android junit mockito powermockito


    【解决方案1】:

    我认为你需要@RunWith(PowerMockRunner.class)。例如:

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({SettingsView.class})
    public class PowermockFinalClassTest {
    
        private SettingsView view;
    
        @Test
        public void testMockingStatic() {
            view = PowerMockito.mock(SettingsView.class);
    
            String expected = "mocked call";
            Mockito.when(view.getSomething()).thenReturn(expected);
    
            Assert.assertEquals(expected, view.getSomething());
        }
    }
    

    【讨论】:

    • 我这样做了,现在我收到以下错误:java.lang.IllegalStateException: Extension API internal error: org.powermock.api.extension.proxyframework.ProxyFrameworkImpl could not be located in classpath.
    • 此异常消息告诉您缺少一个必需的类,可能是您的项目中没有包含 Android 开发所需的所有 PowerMock 库。或者,您可能会遇到this question 中描述的问题。
    • 这是我的 gradle 文件中的 powemock 依赖项:androidTestCompile "org.powermock:powermock-module-junit4:1.6.5"androidTestCompile "org.powermock:powermock-module-junit4-rule:1.6.5"androidTestCompile 'org.powermock:powermock-api-mockito:1.6.5'
    • 您使用的是哪个版本的 Mockito?你使用的是什么版本的 JUnit?
    • 我的 mockito 版本包含在 org.powermock:powermock-api-mockito:1.6.5 中,我使用的是 junit 4
    【解决方案2】:

    你已经模拟了 SettingsView @Mock private SettingsView view;

    你为什么要双重嘲笑它? @Mock and mock 是一样的。

    你还需要模拟对象而不是类本身。

    【讨论】:

    • 取出第二份声明。结果还是一样
    • 你需要删除mock(SettingsView.class),你需要模拟对象而不是类本身
    猜你喜欢
    • 2016-11-06
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    相关资源
    最近更新 更多