为了描述我遇到的问题,我们假设有一个虚拟类:
import static java.lang.System.exit
class Example {
void methodGeneratingSystemExit1() {
exit 1
}
void methodGeneratingSystemExit2() {
exit 2
}
}
并对其进行测试:
import org.junit.Test
import org.junit.Rule
import org.junit.contrib.java.lang.system.ExpectedSystemExit
class ExampleTest {
@Rule
public final ExpectedSystemExit expectedSystemExit = ExpectedSystemExit.none()
@Test
void "System exits with code 1 when method1 is generated"() {
expectedSystemExit.expectSystemExitWithStatus(1)
methodGeneratingSystemExit1()
}
@Test
void "System exits with code 1 when method1 is generated"() {
expectedSystemExit.expectSystemExitWithStatus(2)
methodGeneratingSystemExit2()
}
}
正如我所说,这只是一个虚拟示例,但仍然测试执行了应该执行的操作 - 当调用 System.exit() 时,测试会注意到这一点并且全是绿色。问题是,我有一个 Surefire 插件,它给我一条消息,例如:
ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.17:test (default-test) on project xtrakter: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.17:test failed: The forked VM terminated without properly saying goodbye. VM crash or System.exit called?
如何在 Surefire 中抑制该错误?或者该问题的任何其他解决方法/解决方案?
最佳答案
您可以通过安装自己的 SecurityManager 来测试 System.exit ,在测试期间实现 checkExit 方法。请参阅Java: How to test methods that call System.exit()?了解更多详情。
关于java - Surefire 由于检查是否在 junit 测试之一中调用了 System.exit() 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25034005/