'Can I add a delay to a call to a Stub method in Spock?

I'm using the Spock Framework to test some Java classes. One thing I need to do is add a delay to a Stub method that I'm calling, in order to simulate a long-running method. Is this possible?

This looks possible using Mockito: Can I delay a stubbed method response with Mockito?. Is it possible using Spock?



Solution 1:[1]

One thing I need to do is add a delay to a Stub method that I'm calling, in order to simulate a long-running method. Is this possible?

It is difficult to say for sure if this is the right thing to do without knowing more about the situation under test but if you are executing a method and want that to result in tying up the current thread for a period to simulate doing work, your mock method could invoke Thread.sleep.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeff Scott Brown