我该如何处理是安装一个安全管理器,该安全管理器在调用System.exit时会引发异常。然后是捕获异常且不会使测试失败的代码。
public class NoExitSecurityManager extends java.rmi.RMISecurityManager{ private final SecurityManager parent; public NoExitSecurityManager(final SecurityManager manager) { parent = manager; } public void checkExit(int status) { throw new AttemptToExitException(status); } public void checkPermission(Permission perm) { }}然后在代码中,如下所示:
catch(final Throwable ex){ final Throwable cause; if(ex.getCause() == null) { cause = ex; } else { cause = ex.getCause(); } if(cause instanceof AttemptToExitException) { status = ((AttemptToExitException)cause).getStatus(); } else { throw cause; }}assertEquals("System.exit must be called with the value of " + expectedStatus, expectedStatus, status);


