简体   繁体   English

Java,Mockito-如何找出调用了哪些模拟?

[英]java, mockito - how can i find out which mocks are called?

I don't really care when the mock is called - pretty much, i'm migrating a bunch of mocks into real world code, because mocks in an integration test are sometimes pretty stupid. 我并不在乎何时调用该模拟-差不多,我正在将一堆模拟迁移到真实世界的代码中,因为集成测试中的模拟有时非常愚蠢。

Anyway, here i am with a gigantic class full of mocks setup - but what i really want are for each test to find out which mocks are used. 无论如何,这里我有一个巨大的类,其中包含完整的模拟设置-但我真正想要的是每个测试都可以找出使用了哪些模拟。

With mockito, can i get it to, i don't know, "log" or print or something whenever a particular "when" is actually executed? 使用Mockito,我是否可以在实际执行特定的“何时”时获取它,我不知道它是“日志”还是打印内容?

Since Mockito 1.9.0 you can use the verboseLogging setting. 从Mockito 1.9.0开始,您可以使用verboseLogging设置。

For example, this: 例如,这:

@Test
public void testLogging() {
  final List<?> mockList = mock(List.class, withSettings().verboseLogging());
  when(mockList.size()).thenReturn(6);

  mockList.size();
}

Produces: 产生:

###### Logging method invocation #1 on mock/spy ######## list.size(); ######模拟/间谍的日志记录方法调用#1 ######## list.size(); invoked: -> at 调用:->在

soanswers.mockito.LogCalls.testLogging(LogCalls.java:35) has returned: "0" (java.lang.Integer) soanswers.mockito.LogCalls.testLogging(LogCalls.java:35)已返回:“ 0”(java.lang.Integer)

###### Logging method invocation #2 on mock/spy ######## stubbed: -> at #/ ####模拟/间谍########的日志记录方法调用#2:-> at

soanswers.mockito.LogCalls.testLogging(LogCalls.java:35) list.size(); soanswers.mockito.LogCalls.testLogging(LogCalls.java:35)list.size(); invoked: -> at soanswers.mockito.LogCalls.testLogging(LogCalls.java:37) has returned: "6" (java.lang.Integer) 在soanswers.mockito.LogCalls.testLogging(LogCalls.java:37)处调用->已返回:“ 6”(java.lang.Integer)

I found an issue on the mockito webpage that suggests this is possible: http://code.google.com/p/mockito/issues/detail?id=148 我在模仿网页上发现了一个问题,表明有可能这样做: http : //code.google.com/p/mockito/issues/detail? id=148

Hi, I've prepared a patch at the following branch. 嗨,我已经在以下分支中准备了一个补丁。 I'd appreciate feedback on anything that needs change. 对于任何需要更改的反馈,我将不胜感激。

http://code.google.com/r/muratknecht-mockito-issue148/ http://code.google.com/r/muratknecht-mockito-issue148/

The API is as agreed: API已达成协议:

Foo foo = mock(Foo.class, withSettings().verboseLogging()); Foo foo =模拟(Foo.class,withSettings()。verboseLogging());

and

Foo foo = mock(Foo.class, withSettings().callback(new RememberingListener())); Foo foo =模拟(Foo.class,withSettings()。callback(new RememberingListener()));

The former will print invocation infos to std out. 前者将打印调用信息以将其输出。 Here an example. 这里举个例子。 This is just a proposal, of course. 当然,这只是一个建议。 The location info (-> at ...), however, is simply what the listener gets, and therefor cannot be changed without exposing Location and its fields. 但是,位置信息(位于...处的->只是听者得到的),因此,如果不公开位置及其字段,就无法更改位置信息。

###### Logging method invocation #3 on mock/spy ######## foo.giveMeSomeString("another string value"); ######模拟/间谍上的日志记录方法调用#3 ######## foo.giveMeSomeString(“另一个字符串值”); -> at ->在

org.mockitousage.debugging.VerboseLoggingOfInvocationsOnMockTest.usage(VerboseLoggingOfInvocationsOnMockTest.java:154) Will return: >some return value< (java.lang.String) Method has been stubbed. org.mockitousage.debugging.VerboseLoggingOfInvocationsOnMockTest.usage(VerboseLoggingOfInvocationsOnMockTest.java:154)将返回:>一些返回值<(java.lang.String)方法已存根。 -> at org.mockitousage.debugging.VerboseLoggingOfInvocationsOnMockTest.usage(VerboseLoggingOfInvocationsOnMockTest.java:149) -> org.mockitousage.debugging.VerboseLoggingOfInvocationsOnMockTest.usage(VerboseLoggingOfInvocationsOnMockTest.java:149)

#

The usage-level tests are InvocationListenerCallbackTest and VerboseLoggingOfInvocationsOnMockTest. 使用级别的测试是InvocationListenerCallbackTest和VerboseLoggingOfInvocationsOnMockTest。 You may want to have a closer look at the latter (see the class comment). 您可能需要仔细研究后者(请参阅课程注释)。 As always with usage-level tests that make assertions regarding the contents of the standard output stream, it gets a bit messy. 与对标准输出流的内容进行断言的使用级别测试一样,它有点混乱。

I tried it out in one of my tests and it seems to do what you want. 我在一项测试中尝试了它,它似乎可以完成您想要的。 You should use the first example that looks like this: 您应该使用第一个示例,如下所示:

Foo foo = mock(Foo.class, withSettings().verboseLogging());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM