我想在测试失败时得到通知。理想情况下,我想知道测试是通过了还是在我的@After注释方法中失败了。我知道它们是一个RunListener,可以用于此目的,但只有在我们使用JunitCore运行测试时才能起作用。如果测试用例失败或类似于RunListener (可与SpringJUnit4ClassRunner一起使用),是否有一种方法可以得到通知?
发布于 2014-02-06 21:28:56
Spring框架提供了一个可用于实现这一目标的TestContext SPI。
基本上,如果您实现TestExecutionListener (或者更好地扩展AbstractTestExecutionListener),您可以实现afterTestMethod(TestContext)方法。通过传入的TestContext,您可以访问抛出的异常(如果有异常的话)。
以下是org.springframework.test.context.TestContext.getTestException()的Javadoc
获取在执行测试方法期间引发的异常。 注意:这是一个可变的属性。 返回:抛出的异常,如果没有抛出异常,则为null。
FYI:您可以通过@TestExecutionListeners注释注册客户侦听器。
致以敬意,
相同的
发布于 2017-02-27 01:05:14
另一种选择是JUnits内置TestWatcher规则。
该规则允许您选择要被通知的内容。下面是来自JUnit Github文档的一个压缩示例
@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() {}
}https://stackoverflow.com/questions/21586409
复制相似问题