0

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"
YBDevi
  • 439
  • 5
  • 22
  • Why do you need to test Androids broadcast receiver? We can assume Android have tested it and it works. – JakeB Mar 20 '20 at 13:38
  • @JakeB in this custom broadcast receiver i have multiple actions there is lot work is happening there, i want to write unit test cases for that using mockito – YBDevi Mar 20 '20 at 13:51
  • You should test the classes that implement that interface. All you are testing here is essentially a Switch statement... – JakeB Mar 20 '20 at 15:01
  • @JakeB to test that interface also i'm unable to do it that interface will trigger from receiver and i'm unable to mock that one. – YBDevi Mar 20 '20 at 15:10
  • The code you have posted doesn't need to be tested. The implementing classes do (what ever they may be). – JakeB Mar 20 '20 at 15:13
  • @JakeB can you suggest any article which can be resemble with my situation, as i mentioned i'm new to mockito. I want reference to write unit test code for activity which had implemented interface. – YBDevi Mar 20 '20 at 15:16
  • If you post an example of a class you want to test i can help, but so far it seems like you're just asking how to use mockito to test anything and there are plenty of resources online to help.. – JakeB Mar 20 '20 at 19:20

0 Answers0