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