I have on custom broadcast receiver class which i'm using in multiple places in application, it is working fine. Now i want to write unit testing using mockito but i'm unable to achieve it. This custom broadcast is communicating with Bind Service.
I have followed some SO questions and google articles nothing help me out.
Following articles i have followed.
Unit testing broadcast receiver
Unit testing for calling broadcast receiver
I have try booth but nothing worked.
Following code will tell how my custom broadcast receiver is.
class CustomBroadcastReceiver(val onResultListener: OnResultListener) :
BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
when (intent!!.action) {
Constants.STATUS_ONE -> {
onResultListener.connectionSuccess()
}
Constants.DATA_AVAILABLE -> {
onResultListener.onDataAvailable(data)
}
Constants.GATT_READ -> {
onResultListener.onRead(readData)
}}
interface OnResultListener {
fun connectionSuccess()
fun connectionFail(isFromConnection: Boolean, isFrom: String = Constants.DEFAULT)
fun onDataAvailable(data: ArrayList<Data>)
fun onCharacteristicRead(data:Data)}}
Above code is just sample i'm doing lot of work in this class, by implementing above interface in activities i'm getting data from receiver.
Everything is working fine but unable to write the mockito unit test, i'm new to mockito i know very little things about mockito,following are the mockito imports i'm using.
testImplementation 'org.mockito:mockito-core:2.24.5'
androidTestImplementation 'org.mockito:mockito-core:2.24.5'
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"