Testing with Thread.sleep

后端 未结 6 1831
囚心锁ツ
囚心锁ツ 2021-01-11 16:28

What are the recommended approaches to using Thread.sleep() to speed up tests.

I am testing a network library with a retry functionality when connection

6条回答
  •  旧巷少年郎
    2021-01-11 16:39

    Make the sleeping time configurable through a setter, and provide a default value. So in your unit tests, call the setter with a small argument (1 for example), and then execute the method that would call Thread.sleep().

    Another similar approach is to make if configurable via a boolean, so that Thread.sleep() isn't called at all if the boolean is set to false.

提交回复
热议问题