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

2022-09-04 05:17:21

我正在为现有的Java Swing应用程序实现一些测试,以便我可以安全地重构和扩展代码而不会破坏任何内容。我从 JUnit 中的一些单元测试开始,因为这似乎是最简单的入门方法,但现在我的首要任务是创建一些端到端测试来执行整个应用程序。

我在每个测试中重新启动应用程序,方法是将每个测试方法放在单独的测试用例中,并在 Ant 的任务中使用该选项。但是,我想在测试中实现的一些用例涉及用户退出应用程序,这导致调用System.exit(0)的方法之一。这被 JUnit 视为错误:。fork="yes"junitjunit.framework.AssertionFailedError: Forked Java VM exited abnormally

有没有办法告诉JUnit,返回码为零退出实际上是可以的?


答案 1

系统规则有一个名为 ExpectedSystemExit 的 JUnit 规则。使用此规则,您可以测试调用 System.exit(...) 的代码:

public class MyTest {
    @Rule
    public final ExpectedSystemExit exit = ExpectedSystemExit.none();

    @Test
    public void systemExitWithArbitraryStatusCode() {
        exit.expectSystemExit();
        /* the code under test, which calls System.exit(...)
         * with an arbitrary status
         */
    }

    @Test
    public void systemExitWithSelectedStatusCode0() {
        exit.expectSystemExitWithStatus(0);
        //the code under test, which calls System.exit(0)
    }
}

系统规则至少需要 JUnit 4.9。

完全披露:我是《系统规则》的作者。


答案 2

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

推荐