It is definitely possible to test asynchronous code by letting the thread wait until the asynchronous code finishes. However, is it encouraged to test asynchronous code. Or instead, should we seperate the logic and only test the synchronous part?
For example, in my app I would make a REST API call to fetch json from server and then I need to parse the json.
There are two approaches:
Test the fetching and parsing as a whole. We can use
MockWebServerto simulate timeout and response.-Cons: it would bring some complexity.
Just test the parsing part, since we cannot control the API call part; it is controlled by
okhttp--Cons: Hard to cover timeout case.