如何测试一个方法需要花费超过X秒的时间(使用JUnit)? [英] How to test that a method should take more than X seconds to finish(with JUnit)?
本文介绍了如何测试一个方法需要花费超过X秒的时间(使用JUnit)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基本上,我需要@Test(timeout=X)
注释的相反行为.
Basically I need the opposite behaviour of the @Test(timeout=X)
annotation.
我要解决的问题是以某种方式检测该方法永远不会结束(作为正确的行为).我假设如果该方法在X秒钟后仍未停止,那么我肯定它将永远不会结束".
The problem I want to solve is to detect in some way that the method never ends (as a right behaviour). I am assuming that if the method didn't stop after X seconds, I am sure "it will never end".
谢谢!
推荐答案
您可以尝试以下方法:
@Test
public void methodDoesNotReturnFor5Seconds() throws Exception {
Thread t = new Thread(new Runnable() {
public void run() {
methodUnderTest();
}
});
t.start();
t.join(5000);
assertTrue(t.isAlive());
// possibly do something to shut down methodUnderTest
}
这篇关于如何测试一个方法需要花费超过X秒的时间(使用JUnit)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文