在JUnit测试中处理System.exit(0)

在JUnit测试中处理System.exit(0),第1张

在JUnit测试中处理System.exit(0)

我该如何处理是安装一个安全管理器,该安全管理器在调用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);


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5501651.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-13
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存