2

嘿,我正在使用 SpringJUnit4ClassRunner。默认情况下,TestContextManager.afterTestMethod() 会非常简短地记录每个异常或断言(只是消息),如下所示:

testMethod = testDataDriven@DataDrivenAbstractTest, testException = [null]]

我想编写一个自定义的 TestExecutionListener 来记录我项目中任何测试引发的所有异常。我不想记住用@TestExecutionListeners 注释我的每个测试类。

我可以以某种方式在全球范围内做到这一点吗?我在正确的轨道上吗?

谢谢。

4

2 回答 2

1

不,你不能在全局范围内这样做,但你可以编写一个使用 的基类,TestExecutionListener并让所有其他测试类扩展它。

或者你可以扩展SpringJUnit4ClassRunner来做你的事情并运行你的测试@Runwith(YourCustomSpringJUnit4ClassRunner.class)

于 2011-01-20T16:39:26.533 回答
0

是的你可以。

请参阅https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html#testcontext-tel-config-automatic-discovery

SpringFactoriesLoader将检测一个META-INF/spring.factories文件。在该文件中,您放置了侦听器:

org.springframework.test.context.TestExecutionListener=com.example.YourCoolTestExecutionListener
于 2021-10-27T15:30:12.363 回答