为了描述我遇到的问题,我们假设有一个虚拟类:
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插件,它给我类似以下消息:
[错误]无法在项目Xtrakter上执行目标org.apache.maven.plugins:maven-surefire-plugin:2.17:test(默认测试):目标org.apache.maven.plugins:maven-surefire-执行默认测试插件:2.17:test失败:分叉的VM在没有正确说再见的情况下终止。 VM崩溃还是System.exit被调用?
如何在surefire中抑制该错误?或任何其他解决方法/解决方案?
最佳答案
Surefire随时不支持测试或任何引用的调用System.exit()的库。
来源:http://maven.apache.org/surefire/maven-surefire-plugin/faq.html#vm-termination
我认为这是不可能解决的,这是核心行为。