简体   繁体   中英

Android Unit testing - Have a test wait on a response

So I've got an Android library that I need to do unit testing on. You pass the library some handlers, and I need to unit test based on the messages sent to those handlers from the library.

The problem is, I don't know how to tell a test to wait until I get a response back. I have a timer checking the response every 10 seconds in my test method and if it sees a response is does an assert, but the test method schedules the timer task and then finishes as successful instead of waiting for an assert in the timer task.

Is there a way to have the test explicitly wait until it runs into an assert before finishing? Or some other way of accomplishing this?

Thanks!

You can create mocks of the handlers. Mocks can/should replace the actual handlers in the unit test of the library. You have full control over mocks, and avoid threading issues (unit testing is damn hard with threading involved).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM