17

I am slowly becoming obsessed with unit testing. I am trying to develop as much software as I can using test-driven development. I am using JUnit to unit test my android applications.

I have been working on an app that uses bluetooth and am having a hard time unit testing it. I have an Activity that uses BluetoothAdapter to obtain a list of paired and discovered devices. Although it works, I would like to know how to unit test it.

To get the list of paired devices, I call getBondedDevices() on the instance of BluetoothAdapter. The problem is I don't know how to stub or mock this method (or any other bluetoothAdapter method that my Activity calls) so I can't test my Activity against different lists of paired devices.

I thought about using Mockito or trying to subclass BluetoothAdapter to somehow stub out the methods I'm interested in, but it's a final class so I can't do either.

Any ideas on how I could test programs that use BluetoothAdapter or other resources that are (as far as I know) difficult or impossible to stub or mock? As another example, how would you test a program that uses sockets?

thanks in advance for any help

aleph_null

aleph_null
  • 5,766
  • 2
  • 24
  • 39
  • An [sscce](http://sscce.org) will help. – gontard Aug 27 '12 at 08:42
  • Have you found a solution or hint of a solution or did you ended up refactoring your existing code to allow something similar to what is proposed below? – Rastikan Apr 01 '15 at 16:14
  • 2
    @Rastikan I can't remember that well, but I may have used a recent version of Robolectric to accomplish this. Nowadays, I don't test anywhere near as much as I should because unit testing support on Android is really really crappy. – aleph_null Apr 01 '15 at 17:56
  • this hopefully changed during last year @aleph_null .. any tips? – Ewoks Aug 02 '16 at 21:18

1 Answers1

6

To test your activity, you may refactor your code. Introduce a BluetoothDeviceProvider with a default implementation.

public interface BluetoothDeviceProvider {
    Set<BluetoothDevice> getBluetoothDevices();
}

public class DefaultBluetoothDeviceProvider implements BluetoothDeviceProvider {
    public Set<BluetoothDevice> getBluetoothDevices() {
        return new BluetoothAdapter.getDefaultAdapter().getBondedDevices();
    }
}

then inject this new interface, in the activity :

public class MyActivity extends Activity {
    private BluetoothDeviceProvider bluetoothDeviceProvider;

    public MyActivity(BluetoothDeviceProvider bluetoothDeviceProvider) {
        this.bluetoothDeviceProvider = bluetoothDeviceProvider;
    }

    protected void onStart() {
        Set<BluetoothDevice> devices = bluetoothDeviceProvider.getBluetoothDevices();
        ... 
    }
    ...
}

Now the activity seems unit-testable. But the BluetoothDevice is still final and you can't inject a mock in your activity. So you have to refactor this new code and introduce a new interface that wraps the BluetoothDevice... -> an abstraction layer upon the core android classes.

At the end the activity behavior can be checked via various unit tests... So the implementations of the newly introduced interfaces remains to test. To do this, you can :

  • keep them not (unit) tested, not a big problem for me since they just do delegation

  • look at PowerMock.

Also check this wiki page about mocking final classes using PowerMock.

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
gontard
  • 28,720
  • 11
  • 94
  • 117
  • 2
    Although wrapping the class in an interface/concrete class is tedious, it may be worth doing for the sake of testing important features. Thanks for your answer. I'll definitely look into PowerMock, any idea if it's compatible with Android? – aleph_null Aug 30 '12 at 04:23
  • I never tried PowerMock on Android, but it seems compatible, look at [this post](http://codingjunkie.net/android-unit-testing/) – gontard Aug 30 '12 at 07:18
  • 1
    I know it is few years later, but Mockito 2.+ can mock final classes now.. :) – Ewoks Nov 15 '16 at 07:31