0

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.

Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
  • 1
    As Gabe says, the problem here is with your design. Use dependency injection to provide dependencies that you can mock. – PPartisan Oct 18 '22 at 08:10

1 Answers1

2

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.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127