Search code examples
androidkotlinunit-testingjunitmockito

How to write unit tests for a Android Kotlin Broadcast Receiving App which does not have return values?


I have developed an android kotlin application. Now I am in the process of creating unit test cases for the application using JUnit and Mockito. This application does not have a UI and runs similarly to a system application. A broadcast is sent to this application which will process the sent data and a response will be broadcasted back to the sent application.

I cannot figure out a way to write a test since this application does not have a return value or an action (like opening an activity).

If it is possible to write a test case in this scenario please mention it. Thank You


Solution

  • There are three types of unit tests:

    • return value test (what does it return?)
    • state test (how does the state change?)
    • interaction test (how does it communicate with something else?)

    If you can't test return values, that leaves the other two types. I describe these in this talk: https://youtu.be/Jzlz3Bx-NzM?t=489. And as I describe in the talk, an interaction test is where we get into injecting stubs and spies (I say "mocks" in the talk):

    • A stub returns preset data back to the System Under Test
    • A spy records how it was called by the System Under Test

    Usually for mobile apps, we are dealing with Requests and Responses communicating with some remote service.

    • Inject a spy to see that the app sends the right Request to whatever the remote service is. Changes in state should result in different Requests.
    • Inject a stub that is set up to return various types of Responses, as if it were the remote service. This lets us simulate various success cases, as well as failure cases.