java - 由于 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);
}
}
解决方案
通常明智的做法是不要过多地依赖库。在这种情况下,您可以在 MailExecutor 类上编写扩展,并使用不调用它的实现覆盖调用 System.exit() 的函数。然后,您可以通过扩展类测试 MailExecutor 类。Michael Feathers 在“有效地使用遗留代码”中描述了这种技术。
推荐阅读
- linux - pthread:exit() 是否显式关闭线程的文件描述符
- c# - SpecFlow.Assist.Dynamic 和 TechTalk.SpecFlow.Assist 的 CreateDynamic 方法有什么区别?
- ios - 如何使用在 swift 4 中具有以下参数的 application/x-www-form-urlencoded 调用发布请求
- mongodb - 如何获取给定值在键列表中的记录
- reactjs - Ionic React 尝试实现过滤搜索栏以按标题过滤预制列表
- nginx - 如何为位置设置 nginx 通配符?
- azure - SNOWFLAKE - AZURE 活动目录集成
- node.js - 使用 Jest 模拟多个服务(SSM、SES、SNS)时出错 - lambda nodejs 功能单元测试
- javascript - 这个 JavaScript 没有显示是有原因的吗?
- c - 当N是C的输入并且它也由输入函数定义时,如何在C中定义变量“N”类型(N将提示用户输入)