Java 如何使用 Mockito 在模拟上显示所有调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24294277/
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
How to use Mockito to show all invocations on a mock
提问by durron597
I have a unit test that is failing and I'm unsure why. I want to be able to see all invocations on the mock that occur in the System Under Test. This is not the behavior that I want for all tests always, simply for a test that I need to quickly tweak to be able to figure out what's wrong.
我有一个单元测试失败,我不确定为什么。我希望能够看到在被测系统中发生的模拟上的所有调用。这不是我想要的所有测试的行为,只是为了我需要快速调整才能找出问题所在的测试。
However, it seems kind of like a hack. Is it possible to do this natively in Mockito, without having to use Thread.currentThread().getStackTrace()
?
然而,这似乎有点像黑客。是否可以在 Mockito 中本地执行此操作,而不必使用Thread.currentThread().getStackTrace()
?
This is not preferred, because the stack trace includes all the other invocations used internally by Mockito.
这不是首选,因为堆栈跟踪包括 Mockito 内部使用的所有其他调用。
采纳答案by MRalwasser
This feature is builtin since Mockito 1.9.5. Just use
此功能自 Mockito 1.9.5 起内置。只需使用
mock(ClassToMock.class, withSettings().verboseLogging())
回答by davidxxx
From Mockito 1.9.5
you can inspect a mock with MockingDetails Mockito.mockingDetails(Object mockToInspect)
.
从 Mockito,1.9.5
您可以使用MockingDetails Mockito.mockingDetails(Object mockToInspect)
.
You can either dig into the MockingDetails
properties by invoking : getMock()
, getStubbings()
, getInvocations()
and so for ... or simply use the printInvocations()
method that returns :
您可以挖掘到MockingDetails
通过调用属性:getMock()
,getStubbings()
,getInvocations()
等了......或者干脆使用printInvocations()
方法,它返回:
a printing-friendly list of the invocations that occurred with the mock object. Additionally, this method prints stubbing information, including unused stubbings. For more information about unused stubbing detection see MockitoHint.
模拟对象发生的调用的打印友好列表。此外,此方法打印存根信息,包括未使用的存根。有关未使用的存根检测的更多信息,请参阅 MockitoHint。
For example with JUnit 5 :
以 JUnit 5 为例:
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.Mock;
import org.mockito.Mockito;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
public class FooTest {
Foo foo;
@Mock
Bar bar;
@Test
void doThat() throws Exception {
Mockito.when(bar.getValue())
.thenReturn(1000L);
// ACTION
foo.doThat();
// ASSERTION
// ...
// add that to debug the bar mock
System.out.println(mockingDetails(bar).printInvocations());
}
}
And you get an output such as :
你会得到一个输出,例如:
[Mockito] Interactions of: Mock for Bar, hashCode: 962287291 1. bar.getValue(); -> at Foo.doThat() (Foo.java:15) - stubbed -> at FooTest.doThat(FooTest.java:18)
Note that the classes with a referenced line in the output are links to your source code/test class. Very practical.
请注意,输出中带有引用行的类是指向源代码/测试类的链接。非常实用。