我有以下代码: class Sleeper { public void sleep(long duration) { try { Thread.sleep(duration); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }} 如果Thread.sleep()抛出InterruptedException,如何使用JM
class Sleeper { public void sleep(long duration) { try { Thread.sleep(duration); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }
如果Thread.sleep()抛出InterruptedException,如何使用JMockit测试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(); }