I have the following code:
class Sleeper {
public void sleep(long duration) {
try {
Thread.sleep(duration);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
How do I test, with JMockit, that Thread.currentThread().interrupt() is called if Thread.sleep() throws an InterruptedException?
Interesting question. A bit tricky to test because mocking certain methods of
java.lang.Threadcan interfere with the JRE or with JMockit itself, and because JMockit is (currently) unable to dynamically mock native methods such assleep. That said, it can still be done: