2

So I am trying to use PowerMockito in place of Mockito so I can mock a class that is final. However, after I switch over to PowerMockito, I get the following stackTrace when trying to mock any class:

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

And here is the setup method:

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

  @Mock private SettingsView view;

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

Does anyone know what could potentially be causing this? I have tried to change mock(SettingsView.class) to PowerMockito.mock(SettingsView.class) but I get the same result.

Also, view is an interface in this scenario.

hermt2
  • 844
  • 3
  • 14
  • 33

2 Answers2

0

You already mock SettingsView @Mock private SettingsView view;

Why are you double mocking it? @Mock and mock are the same.

Also you need to mock object not class itself.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
0

I think you need to @RunWith(PowerMockRunner.class). For example:

@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());
    }
}
glytching
  • 44,936
  • 9
  • 114
  • 120
  • I did this, now I get the following error: `java.lang.IllegalStateException: Extension API internal error: org.powermock.api.extension.proxyframework.ProxyFrameworkImpl could not be located in classpath.` – hermt2 Aug 15 '17 at 14:54
  • This exception message tells you that your are missing a required class, perhaps you haven't included all the PowerMock libraries required for Android development in your project. Alternatively you may be encountering the issue described in [this question](https://stackoverflow.com/questions/15305490/how-to-use-powermock-in-android-projects). – glytching Aug 15 '17 at 15:04
  • Here are the powemock dependencies in my gradle file: `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'` – hermt2 Aug 15 '17 at 15:18
  • What version of Mockito are you using? What version of JUnit are you using? – glytching Aug 15 '17 at 15:22
  • my version of mockito is wrapped in `org.powermock:powermock-api-mockito:1.6.5` and I am using junit 4 – hermt2 Aug 15 '17 at 15:25
  • `powermock-api-mockito` does **not** wrap Mockito. You'll need to include the Mockito library too. Also, there are version compatability issues between PowerMock, Mockito and JUnit so the particular version of JUnit 4 is important. See the docs [here](https://github.com/powermock/powermock/wiki/Mockito-2-Maven) and [here](https://github.com/powermock/powermock/wiki/Mockito#supported-versions). – glytching Aug 15 '17 at 15:34
  • When I add this: `androidTestCompile 'org.mockito:mockito-android:2.7.22'` Then I get a build error saying that Duplicate files were copied in APK-mockito-extensions – hermt2 Aug 15 '17 at 15:42
  • This comment section is getting too long at this stage :) It's possible that your latest comment is addressed by this [answer](https://stackoverflow.com/questions/35468896/using-powermock-and-mockito-in-an-android-instrumentation-test-error-duplica#39214994). Either way, I think the original question posted above has been answered by now. – glytching Aug 15 '17 at 15:45