【发布时间】:2021-10-02 10:53:14
【问题描述】:
我使用 Spring Boot 编写了一个 Cucumber 测试来测试另一个 maven 模块项目 XX 功能。 Project XX 有许多微主要功能,它们作为 jar 运行并被不同的团队使用。测试用例运行良好,但项目是每个微功能在成功执行结束时都使用System.exit(statusCode),这显然停止了我的测试用例
想知道是否有任何方法可以防止我的弹簧靴从System.exit(statusCode) 退出。我正在使用 Spring Boot 2.5.4
有人可以帮我解决这个问题吗?
更新 1
根据 Gaël J 的建议,我使用了System Rules,并在我的黄瓜步骤定义中尝试如下所示,但它仍然不起作用
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);
}
}
【问题讨论】:
-
如果你完全使用 JUnit,你可以使用 JUnit 退出断言规则(有一个库提供)。
-
@GaëlJ 我试过了还是不行,你能检查我的更新 1
标签: java spring spring-boot cucumber