Skip to content

Improve testability #45

Open
Open
@ghost

Description

Given the current code, we are very limited on how much we can test our BleManager descendants.

  • We can't unit test it because the constructor initializes a Handler from the main looper, so we are forced to use AndroidJUnit4 runner
  • When running on AndroidJUnit4, Mockito can't mock final classes. I'd like to not include PowerMock, but I may end up doing that.
    • We can't test our BleManagerGattCallback, which receives BluetoothGatt as a parameter. And we can't move the implementation to an independent class because we have to fetch & store the GattCharacteristics to be used later
    • We can't test our bluetooth operations, which throw InvalidRequestException because mBluetoothDevice is null. And that's probably only the tip of the iceberg, I haven't gone past that point.

Making the code testable would be a daunting task, but maybe we could start with something simple such as exposing a constructor that takes a Handler as parameter. There's a VisibleForTesting annotation that will flag the usage as a lint error if used outside from test sourceset. VisibleForTesting lives in an independent package (com.android.support:support-annotations:XXX, maybe it's androidx now?)

I'm eager to listen to other ideas, I'm sure I'm not the only one having this issue.

Thanks!

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions