java – Mockito吞下堆栈跟踪?

前端之家收集整理的这篇文章主要介绍了java – Mockito吞下堆栈跟踪?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
为什么Mockito吞下堆栈痕迹?例如,如果我有一个
public class Foo
{
    public void foo()
    {
        bar();
    }

    public void bar()
    {
        baz();
    }

    public void baz()
    {
        throw new RuntimeException();
    }
}

和一个测试例如

public class MockTest
{
    @Test
    public void test()
    {
        Mockito.spy(new Foo()).foo();
    }
}

抛出的异常总是看起来像

java.lang.RuntimeException
    at Foo.baz(Foo.java:17)
    at MockTest.test(MockTest.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

所以,所有的东西在哪里

Foo.baz(Foo.java:17)
MockTest.test(MockTest.java:11)

(这里提供的示例只是一个简化 – 我正在处理更多的indirections,类等,我不能让Mockito吞噬部分模拟堆栈跟踪的关键部分…)

解决方法

是的,mockito清理堆栈跟踪!

这段代码在工作StackTraceFilter

有不同的方法来禁用它

>自Mockito 1.10.10以来,通过mockito扩展机制提供您自己的StackTraceCleanerProvider(创建一个资源文件mockito-extensions / org.mockito.plugins.StackTraceCleanerProvider,具有您实现的限定名称)
>在自定义的IMockitoConfiguration中覆盖cleaningStackTrace方法,查看there了解更多信息.

猜你在找的Java相关文章