0

This app shows you can use createMock(BluetoothAdapter.class): https://github.com/riis/AndroidArduino.git

So to try that, I add EasyMock and PowerMock to my build.gradle:

    androidTestImplementation 'org.powermock:powermock-core:1.6.5'
    androidTestImplementation 'org.powermock:powermock-module-junit4:1.6.5'
    androidTestImplementation 'org.powermock:powermock-api-easymock:1.6.5'
    androidTestImplementation 'org.easymock:easymock:4.0.2'

Then I import the mocker, import static org.powermock.api.easymock.PowerMock.createMock;, and try to use it in a test:

    @Test
    public void testWeEnsureBluetoothIsEnabled() {
        ItemListActivity.adapter = createMock(BluetoothAdapter.class);

    }

That causes this inscrutable exception:

java.lang.NoClassDefFoundError: Failed resolution of: Lorg/easymock/internal/MocksControl$MockType;

So, while I implement the Bluetooth layer using Code-and-Fix instead of TDD, does anyone know how to mock the BluetoothAdapter, despite it is a final class?


After upgrading to PowerMock 2, the exception changes to this:

java.lang.BootstrapMethodError: Exception from call site #2 bootstrap method
at org.easymock.internal.ObjectMethodsFilter.<clinit>(ObjectMethodsFilter.java:32)
at org.easymock.internal.MocksControl.createMock(MocksControl.java:98)
at org.easymock.internal.MocksControl.createMock(MocksControl.java:73)
at org.powermock.api.easymock.PowerMock.doCreateMock(PowerMock.java:2023)
at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:1970)
at org.powermock.api.easymock.PowerMock.createMock(PowerMock.java:95)
at com.allflat.planarinfinity.ItemListActivityTest.testWeEnsureBluetoothIsEnabled(ItemListActivityTest.java:69)
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.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:531)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
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 androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)
Caused by: java.lang.ClassCastException: Bootstrap method returned null
... 37 more
Phlip
  • 5,253
  • 5
  • 32
  • 48

1 Answers1

0

The Powermock version is too old. It uses a type that is now removed from EasyMock. Upgrade to Powermock 2 or downgrade EasyMock to 3.

Henri
  • 5,551
  • 1
  • 22
  • 29
  • Switching to `androidTestImplementation 'org.powermock:powermock-core:2.0.0' androidTestImplementation 'org.powermock:powermock-module-junit4:2.0.0' androidTestImplementation 'org.powermock:powermock-api-easymock:2.0.0'` gives me a "java.lang.BootstrapMethodError: Exception from call site #2 bootstrap method". How do I find the latest version of these libraries? – Phlip Jun 21 '19 at 23:26
  • Do you have a stack trace? – Henri Jun 23 '19 at 01:23
  • Highly interesting. Which JVM are you using? I'm suspecting the cglib version now. And if you can give me a github repo featuring the error, it will be awesome. – Henri Jun 25 '19 at 01:53
  • I put a MyApplication project here: https://github.com/Phlip/powermock_error.git . Its test `useAppContext` contains a line that fails, `createMock(BluetoothDevice.class)`. – Phlip Jun 28 '19 at 19:02
  • Hum... Now we are in the wonderful realm of Android. Which command are you launching? On which version of Android? – Henri Jun 30 '19 at 04:47
  • I managed to reproduce. It's hitting a lambda and gets unhappy. It looks like a weird bug between PowerMock and Android. Will dig a little. – Henri Jul 03 '19 at 12:50
  • Can you file an issue in EasyMock so I won't forget and you can track progress? – Henri Jul 12 '19 at 01:35