Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用SpringJUnit4ClassRunner使用RunNotifier或RunListener_Java_Spring_Junit_Spring Test_Spring Junit - Fatal编程技术网

Java 如何使用SpringJUnit4ClassRunner使用RunNotifier或RunListener

Java 如何使用SpringJUnit4ClassRunner使用RunNotifier或RunListener,java,spring,junit,spring-test,spring-junit,Java,Spring,Junit,Spring Test,Spring Junit,我想在测试失败时得到通知。理想情况下,我想知道@After注释方法中的测试是通过还是失败。我知道他们是一个RunListener,可以用于此目的,但只有在我们使用JunitCore运行测试时,它才起作用。如果测试用例失败或类似于RunListener的东西可以与SpringJUnit4ClassRunner一起使用,是否有办法得到通知?Spring TestContext框架提供了一个TestExecutionListenerSPI,可用于实现这一点 基本上,如果实现TestExecutionL

我想在测试失败时得到通知。理想情况下,我想知道@After注释方法中的测试是通过还是失败。我知道他们是一个RunListener,可以用于此目的,但只有在我们使用JunitCore运行测试时,它才起作用。如果测试用例失败或类似于RunListener的东西可以与SpringJUnit4ClassRunner一起使用,是否有办法得到通知?

Spring TestContext框架提供了一个
TestExecutionListener
SPI,可用于实现这一点

基本上,如果实现
TestExecutionListener
(或者更好地扩展
AbstractTestExecutionListener
),则可以实现
PostTestMethod(TestContext)
方法。从传入的
TestContext
中,您可以访问抛出的异常(如果有)

以下是
org.springframework.test.context.TestContext.getTestException()的Javadoc

获取在测试方法执行期间引发的异常

注意:这是一个可变属性

返回:引发的异常,如果未引发异常,则返回null 扔

仅供参考:您可以通过
@TestExecutionListeners
注释注册您的客户侦听器

问候,


Sam

Spring TestContext框架提供了一个可用于实现此目的的
TestExecutionListener
SPI

基本上,如果实现
TestExecutionListener
(或者更好地扩展
AbstractTestExecutionListener
),则可以实现
PostTestMethod(TestContext)
方法。从传入的
TestContext
中,您可以访问抛出的异常(如果有)

以下是
org.springframework.test.context.TestContext.getTestException()的Javadoc

获取在测试方法执行期间引发的异常

注意:这是一个可变属性

返回:引发的异常,如果未引发异常,则返回null 扔

仅供参考:您可以通过
@TestExecutionListeners
注释注册您的客户侦听器

问候,


Sam

另一种选择是JUnits内置的TestWatcher规则

该规则允许您选择要通知的内容。下面是一个来自


另一种选择是JUnits内置的
TestWatcher
规则

该规则允许您选择要通知的内容。下面是一个来自


非常感谢Sam。我现在正在使用testexecutionlistener。非常感谢Sam。我现在正在使用testexecutionlistener。
@Test 
public class WatcherTest {

  @Rule
  public final TestRule watcher = new TestWatcher() {

    @Override
    protected void failed(Throwable e, Description) {}

    // More overrides exists for starting, finished, succeeded etc
  };

  @Test
  public void test() {}
}