I have to unit test a method of a groovy class which uses sleep() to delay processing in a loop.
Obviously I don’t want my test suite to sleep really, so I tried to mock the sleep() call in my class:
MyClass.metaClass.sleep = { return }
I tried a few variations of this with no success.
Can someone tell me the correct way to mock this language method?
You need to be explicit in the arguments, and match the method you’re overriding explicitly. Relying on the default “it” parameter doesn’t work. Notice that overriding the static method only works when we state that the parameter is a “long” (and it’s a static method, not an instance method):