SweetUnit Testing Module

SweetUnit allows you to unit test your BleDevices in your app. The module provides pre-built configurations for "happy path" tests. It is up to you to implement failure conditions.

Prerequisite The module was built with the assumption that Robolectric would be used. See Getting-Started-Sweetunit

It is recommended to create a base test class from which all of your test classes will extend.

// Parameterize the Test class with the Activity you want to test against
public class MyBaseTest extends SweetUnitTest<MyActivity>

    static final String KEY = "YOUR_API_KEY_HERE";

    protected Activity createActivity()
        return Robolectric.setupActivity(MyActivity.class);

    protected String getKey()
        return KEY;


And now an example of reading the battery level of a device

@Config(manifest = Config.NONE, sdk = 25)
public class BatteryTest extends MyBaseTest

    // The database of the device we're going to interact with.
    GattDatabase db = new GattDatabase()
            .setValue(new byte[] { (byte) 100 })

    public void readBatteryTest() throws Exception
        // Logging is off by default. Turning it on for this test
        mConfig.loggingOptions = LogOptions.ON;

        // Now we can just apply mConfig to the manager instance, and logs will be turned on

        // Create a new BleDevice, with a random mac address, and a name
        final BleDevice device = m_mgr.newDevice(Util_Unit.randomMacAddress(), "ABatteryPoweredDevice");

        // Now connect to the device, and read it's battery level
        device.connect(connectEvent -> {
            BleRead read = new BleRead(Uuids.BATTERY_SERVICE_UUID, Uuids.BATTERY_LEVEL).setReadWriteListener(readEvent -> {
                // You must call succeed when the test should be ended successfully. If any of the assert methods throw an
                // exception, it will end the test.

        // This method blocks the test thread (different from the UI thread). You MUST call succeed() when the test is to exit after
        // a success.