Ela*_*nda 1 java junit exception error-code runtimeexception
这是我的主要方法:
public static void main(String... args) throws ClassNotFoundException {
String[] classAndMethod = args[0].split("#");
if (helpCmd(args)) {
printHelpForTest(classAndMethod[1]);
System.exit(0);
}
Result result = runTest(classAndMethod);
exitIfTimeOutExceptionThrown(result);
System.exit(result.wasSuccessful() ? 0 : 1);
}
private static void exitIfTimeOutExceptionThrown(Result result) {
boolean isTimeOut = Iterables.filter(result.getFailures(), CodeBlockTimeOutException.class).iterator().hasNext();
if (isTimeOut)
{
System.exit(2);
}
}
Run Code Online (Sandbox Code Playgroud)
运行此junit测试:
@Test
public void sendSearchRequest() throws Exception {
...
setTimeOut();
...
}
private void setTimeOut() {
if (criticalBlockTimeOut != null) {
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
throw new CodeBlockTimeOutException();
//thread.interrupt();
}
};
new Timer().schedule(timerTask, Long.parseLong(criticalBlockTimeOut));
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我看到我的代码抛出CodeBlockTimeOutException但它主要永远不会退出代码2?
我怎么能把它扔进主线程?
计时器事件发生在另一个线程 主线程退出没有错误.从另一个记录错误.如果出现异常,您应该从预定作业中设置一些易失性标志.等待作业在主线程中完成,然后检查此标志.
看看这里:等待Timer完成Java或Java:在继续执行之前等待TimerTask完成
| 归档时间: |
|
| 查看次数: |
100 次 |
| 最近记录: |