Android BluetoothAdapter Mocking

I'm trying to fake a test bluetooth application, but my first step in creating a blueprint for an object of class BluetoothAdapter does not work!

I use powermockito with a light layout.

mBluetoothAdapter = (BluetoothAdapter)PowerMock.createMock(BluetoothAdapter.class); 

it fails. with the next stack trace

 java.lang.IllegalArgumentException: No visible constructors in class android.bluetooth.BluetoothAdapter at org.easymock.internal.DefaultClassInstantiator.getConstructorToUse(DefaultClassInstantiator.java:94) at org.easymock.internal.AndroidClassProxyFactory.createProxy(AndroidClassProxyFactory.java:48) at org.easymock.internal.MocksControl.createMock(MocksControl.java:114) at org.easymock.internal.MocksControl.createMock(MocksControl.java:88) at org.easymock.internal.MocksControl.createMock(MocksControl.java:79) at org.powermock.api.easymock.PowerMock.doCreateMock(PowerMock.java:2212) at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:2163) at org.powermock.api.easymock.PowerMock.createMock(PowerMock.java:89) at com.xxx.blesimplesample.test.MainActivityTest.setUp(MainActivityTest.java:59) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1710) 

Has anyone used some kind of mocking bluetooth app structure? Any suggestions would be helpful.

+6
source share
1 answer

BluetoothAdapter in the Android framework is declared final , so at the time you asked this question, it could not be mocked, either with Mockito or with Robolectric.

However, testing of Android modules has changed a lot since then. When using the latest versions of the tools when building unit tests, the tools generate a fixed android.jar with all final removed. This makes all Android classes accessible to ridicule. Currently, if you want to make fun of any Bluetooth code, you can do it in a standard way. The code that you have already tried will work if you update the latest tools. In addition, Robolectric now has the ShadowBluetoothAdapter class.

+1
source

Source: https://habr.com/ru/post/977701/


All Articles