java 如何启用 Mockito 调试消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11802088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:25:37  来源:igfitidea点击:

How do I enable Mockito debug messages?

javadebuggingtestingmockito

提问by Derek Mahar

How do I enable Mockitodebug messages? In particular, I'd like print the details of methods stubbed with when()and every interaction with those methods.

如何启用Mockito调试消息?特别是,我想打印存根方法的详细信息when()以及与这些方法的每次交互。

回答by Brice

Mockito 1.9.0 introduced listeners and now bundles a verbose logger:

Mockito 1.9.0 引入了监听器,现在捆绑了一个详细的记录器:

So basically if you want simple and stupid logs, just do the following:

所以基本上如果你想要简单而愚蠢的日志,只需执行以下操作:

List mockWithLogger = mock(List.class, withSettings().verboseLogging());

See http://docs.mockito.googlecode.com/hg/latest/org/mockito/MockSettings.html#verboseLogging() for more information

有关详细信息,请参阅http://docs.mockito.googlecode.com/hg/latest/org/mockito/MockSettings.html#verboseLogging()

Cheers,

干杯,

回答by estani

Brice answer is the way to go, but another option is:

Brice 的答案是要走的路,但另一种选择是:

new org.mockito.internal.debugging.MockitoDebuggerImpl().printInvocations(mockedObject);

Which just prints the interactions that happened prior to that point to the given option. Is not robust enough, but does the trick and might be useful for some cases (i.e. when using mock annotations)

它只是将在该点之前发生的交互打印到给定选项。不够健壮,但可以解决问题并且可能对某些情况有用(即使用模拟注释时)

For Example this should work according to MockitoSettings

例如,这应该根据MockitoSettings工作

spiedObject = mock(ToMock.class, withSettings().spiedInstance(toMockInstance).verboseLogging())

Though in 1.9.5 doesn't seem to spy anything, it just mocking it.

尽管在 1.9.5 中似乎没有监视任何东西,但它只是在嘲笑它。