Skip to content
Advertisement

How to stop main method calling sleep() method, when testing it with Junit?

I am testing a method which contain sleep in side it.What is the way to stop invoke sleep() as it makes testing slow??

JavaScript

Advertisement

Answer

There is no easy way for doing this. You have several options.

The easiest one would be to make the REQUEST_STATUS_CHECK_INTERVAL configurable and configure it to 0 in tests. It can be a property of the tested class.

JavaScript

In the test would wold call

JavaScript

Second option would be to extract the sleep call into it’s own class that can be mocked.

JavaScript

In your class you would have

JavaScript

In the function

JavaScript

And it the test you can do

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement