How to Mock HandlerThread in Android

214 views Asked by At

How can i mock HandlerThreadin Android. I am creating HandlerThread in constructor of a class. I need to mock the HandlerThread to write test case. Here is my code

 HandlerThread("TimeoutHandlerThread").apply {
    start()
    workHandler = WorkHandler(looper)
}

Didn't find any clue.

How can i mock this

Thanks in advance.

1

There are 1 answers

0
Gabe Sechan On

You can't mock something internal to a class. You can mock things you pass into the class. The solution to mocking this is not to create the HandlerThread in the class but to pass it in as a parameter to the constructor. Then it can be mocked however you wish.