首页 > 解决方案 > 由于 System.exit,Spring Boot Cucumber 测试正在退出

问题描述

我使用 Spring Boot 编写了一个 Cucumber 测试来测试另一个 maven 模块项目 XX 功能。Project XX 有许多微主要功能,它们作为 jar 运行并被不同的团队使用。测试用例运行良好,但项目是System.exit(statusCode)在成功执行结束时使用的每个微功能,这显然停止了我的测试用例

想知道是否有任何方法可以防止我的弹簧靴退出System.exit(statusCode)。我正在使用 Spring Boot 2.5.4

有人可以帮我解决这个问题。

更新 1

根据Gaël J的建议,我使用了系统规则并尝试如下所示在我的黄瓜步骤定义中,但它仍然不起作用

import org.junit.Rule;
import org.junit.contrib.java.lang.system.Assertion;
import org.junit.contrib.java.lang.system.ExpectedSystemExit;

public class StepDefs{

    @Rule
    public final ExpectedSystemExit exit = ExpectedSystemExit.none();
    :
    :
    :
    @When("^the function is executed$")
    public void functtionIsExecuted() {

    exit.expectSystemExitWithStatus(0);
        exit.checkAssertionAfterwards(new Assertion() {
            @Override
            public void checkAssertion() throws Exception {
                System.out.println("This is executed AFTER System.exit() method!");
            }
        });
        System.out.println("This is executed right before System.exit().");
        String args = {"some args"}
        new MailExecutor().main(args);
    }
} 

标签: javaspringspring-bootcucumber

解决方案


通常明智的做法是不要过多地依赖库。在这种情况下,您可以在 MailExecutor 类上编写扩展,并使用不调用它的实现覆盖调用 System.exit() 的函数。然后,您可以通过扩展类测试 MailExecutor 类。Michael Feathers 在“有效地使用遗留代码”中描述了这种技术。


推荐阅读