java.lang.ExceptionInInitializerError-PowerMockito

java.lang.ExceptionInInitializerError-PowerMockito,java,android,junit,mockito,powermockito,Java,Android,Junit,Mockito,Powermockito,所以我试图用PowerMockito代替Mockito,这样我可以模拟一个最终类。但是,在切换到PowerMockito之后,在尝试模拟任何类时,我会得到以下stackTrace: java.lang.ExceptionInInitializerError at org.powermock.api.mockito.repackaged.ClassImposterizer.createProxyClass(ClassImposterizer.java:95) at org.powermock.ap

所以我试图用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
有人知道这可能是什么原因吗?我尝试将
mock(SettingsView.class)
更改为
PowerMockito.mock(SettingsView.class)
,但得到了相同的结果


此外,视图在此场景中是一个接口。

您已经模拟了设置视图
@mock private settings视图

你为什么要双重嘲弄它?都是一样的


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

我认为您需要
@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());
    }
}

拿出第二份声明。仍然是相同的结果您需要删除
mock(setingsview.class)
,您需要模拟对象而不是它自己的类我这样做了,现在我得到以下错误:
java.lang.IllegalStateException:Extension API内部错误:org.powermock.API.Extension.proxyframework.ProxyFrameworkImpl在类路径中找不到。
此异常消息告诉您缺少所需的类,也许您还没有在项目中包含Android开发所需的所有PowerMock库。或者,您可能会遇到中描述的问题。以下是我的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
中,我使用的是JUnit4
@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());
    }
}