本文介绍了Surefire失败,因为检查了其中一个junit测试中是否调用了System.exit()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为描述我遇到的问题,让我们假设有一个虚拟类:

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:

如何在surefire中抑制该错误?或该问题的其他解决方法/解决方案?

How can I suppress that error in surefire? Or any other workaround/solution to that problem?

推荐答案

源: 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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 05:23