Noe*_*Yap 6 static-methods jmockit junit4
我有以下代码:
class Sleeper {
public void sleep(long duration) {
try {
Thread.sleep(duration);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用JMockit测试如果Thread.sleep()抛出InterruptedException,则调用Thread.currentThread()。interrupt()吗?
有趣的问题。测试起来有点棘手,因为模拟 的某些方法java.lang.Thread可能会干扰 JRE 或 JMockit 本身,并且因为 JMockit(当前)无法动态模拟本机方法,例如sleep. 也就是说,仍然可以做到:
public void testResetInterruptStatusWhenInterrupted() throws Exception
{
new Expectations() {
@Mocked({"sleep", "interrupt"}) final Thread unused = null;
{
Thread.sleep(anyLong); result = new InterruptedException();
onInstance(Thread.currentThread()).interrupt();
}
};
new Sleeper.sleep();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3563 次 |
| 最近记录: |