Surefire失败,因为检查了其中一个junit测试中是否调用了System.exit() [英] Surefire fails because of checking if System.exit() was called in one of junit tests
问题描述
为描述我遇到的问题,让我们假设有一个虚拟类:
To depict the problem I encountered, let's assume there is a dummy class:
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插件,它给我类似以下消息:
As I said, it's just dummy example but still test does what is supposed o do - when System.exit() is called, test notices that and is all green. The problem is, I have a surefire plugin that gives me a message like:
错误]无法在项目Xtrakter上执行目标org.apache.maven.plugins:maven-surefire-plugin:2.17:test(默认测试):执行目标org.apache.maven.plugins:maven的默认测试-surefire-plugin:2.17:test失败:分叉的VM在没有正确说再见的情况下终止.VM崩溃还是System.exit被调用?
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中抑制该错误?或该问题的其他解决方法/解决方案?
How can I suppress that error in surefire? Or any other workaround/solution to that problem?
推荐答案
Surefire随时都不支持测试或任何引用的调用System.exit()的库.
Surefire does not support tests or any referenced libraries calling System.exit() at any time.
源: http://maven.apache.org/surefire/maven-surefire-plugin/faq.html#vm-termination
我认为不可能解决这个问题,这是核心行为.
I think it's impossible to solve this problem, it's surefire core behaviour.
这篇关于Surefire失败,因为检查了其中一个junit测试中是否调用了System.exit()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!