@Test (expected=TimeoutException.class,timeout=1000)
public void fineForFiveSeconds() {
foo.doforever();
fail("This line should never reached");
}
这是我的测试代码。
我想要的只是运行 doforever()
一段时间,然后使测试成功。
最佳答案
试试这个:
在线程中执行逻辑, hibernate 并检查线程是否还活着。
@Test
public void fineForFiveSeconds() throws InterruptedException {
Thread thread = new Thread() {
@Override
public void run() {
foo.doforever();
}
};
thread.start();
//Let the current thread sleep (not the created thread!)
Thread.sleep(5000);
assertTrue(thread.isAlive());
}
关于java - 如何使用 Junit 和超时获得成功?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26741710/