【发布时间】:2023-03-05 18:58:01
【问题描述】:
这是我的主要方法:
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);
}
}
运行这个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));
}
}
为什么我看到我的代码抛出了 CodeBlockTimeOutException 但它永远不会以代码 2 退出?
我怎样才能把它扔到主线程中?
【问题讨论】:
标签: java exception junit runtimeexception error-code