Maven Surefire配置trimStackTrace为false仍无法获取所有失败测试完整栈跟踪
问题描述
已将Maven Surefire的trimStackTrace参数设为false,想拿到测试失败时的完整栈跟踪,但最终生成的测试报告里,只有部分失败测试附带了完整栈信息。
当前环境:
- JUnit 4.12
- Maven Surefire 3.0.0-M5
同属cc.mallet包的同一测试类下,两个失败用例的输出存在明显差异:test 13生成了完整栈,test 16没有达到预期的栈完整度。
实际输出对比
test 13 栈输出
java.lang.AssertionError at cc.mallet.types.FeatureSequence.add(FeatureSequence.java:129) at cc.mallet.types.FeatureSequence.<init>(FeatureSequence.java:48) at cc.mallet.types.HashedSparseVector_ESTest.test13(HashedSparseVector_ESTest.java:559) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:288) at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:282) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.lang.Thread.run(Thread.java:748)
test 16 栈输出
java.lang.AssertionError: expected:<1.0> but was:<1825.52014> at org.junit.Assert.fail(Assert.java:89) at org.junit.Assert.failNotEquals(Assert.java:835) at org.junit.Assert.assertEquals(Assert.java:555) at org.junit.Assert.assertEquals(Assert.java:685) at cc.mallet.types.HashedSparseVector_ESTest.test16(HashedSparseVector_ESTest.java:735) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:288) at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:282) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.lang.Thread.run(Thread.java:748)
可落地的处理方案
对比两份栈内容可以发现,test 16的栈并未被Surefire裁剪,只是本身没有包含业务类的深层调用片段:test 16的断言直接在测试方法内触发,异常从JUnit的Assert方法抛出后直接走到测试方法,没有经过更深的业务代码调用层级,自然不会出现类似test 13中FeatureSequence类的栈片段。如果需要确保所有失败测试的栈100%完整透传到surefire-reports目录的报告中,可按以下配置调整:
- 除已设置的
trimStackTrace=false外,补全关闭所有栈裁剪逻辑的配置:- 将
trimFullStackTrace设为false,关闭默认对JUnit框架内部调用栈的自动裁剪 - 将
trimResultSet设为false,避免报告生成阶段对栈内容做格式化裁剪 - 手动将
stackTraceDenyList配置为空数组,避免内置的过滤规则误删栈片段
- 将
- 排查JUnit依赖冲突:3.0.0-M5版本的Surefire会自动扫描类路径下的JUnit版本,若存在传递依赖引入的多版本JUnit,会导致部分异常被默认异常处理器截断栈信息。可通过
mvn dependency:tree | grep junit检查依赖树,确保JUnit 4.12为唯一生效版本,排除其他版本的冲突。 - 关闭超时场景的栈隔离逻辑:当前两个测试都启用了JUnit的
FailOnTimeout超时执行器,该执行器会在独立线程中运行测试用例,若异常被线程池包装,会丢失部分调用栈。可在Surefire配置中设置forkCount=1、reuseForks=true,确保异常栈从抛出点到测试入口完整透传,不被线程包装逻辑截断。
内容的提问来源于stack exchange,提问作者sijal
相关产品推荐
相关产品推荐

