我正在寻找一种使用 Mockito 验证的方法,即在测试期间与给定的模拟没有任何交互.对于具有验证模式 never() 的给定方法,很容易实现这一点,但我还没有找到完整模拟的解决方案.
I'm looking for a way to verify with Mockito, that there wasn't any interaction with a given mock during a test. It's easy to achieve that for a given method with verification mode never(), but I haven't found a solution for the complete mock yet.
我真正想要实现的目标:在测试中验证,没有任何东西打印到控制台.jUnit 的总体思路是这样的:
What I actually want to achieve: verify in tests, that nothing get's printed to the console. The general idea with jUnit goes like that:
private PrintStream systemOut;
@Before
public void setUp() {
// spy on System.out
systemOut = spy(System.out);
}
@After
public void tearDown() {
verify(systemOut, never()); // <-- that doesn't work, just shows the intention
}
一个 PrintStream 有很多方法,我真的不想用单独的验证来验证每一个方法 - System.err 也是如此...
A PrintStream has tons of methods and I really don't want to verify each and every one with separate verify - and the same for System.err...
所以我希望,如果有一个简单的解决方案,我可以,考虑到我有很好的测试覆盖率,强制软件工程师(和我自己)删除他们的(我的)调试代码,比如 System.out.println("Breakpoint#1"); 或 e.printStacktrace(); 在提交更改之前.
So I hope, if there's an easy solution, that I can, given that I have a good test coverage, force the software engineers (and myself) to remove their (my) debug code like System.out.println("Breakpoint#1"); or e.printStacktrace(); prior to committing changes.
使用这个:
import static org.mockito.Mockito.verifyZeroInteractions;
// ...
private PrintStream backup = System.out;
@Before
public void setUp() {
System.setOut(mock(PrintStream.class));
}
@After
public void tearDown() {
verifyZeroInteractions(System.out);
System.setOut(backup);
}
这篇关于Mockito - 如何验证从未调用过模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何检测 32 位 int 上的整数溢出?How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
return 语句之前的局部变量,这有关系吗?Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
如何将整数转换为整数?How to convert Integer to int?(如何将整数转换为整数?)
如何在给定范围内创建一个随机打乱数字的 intHow do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
java的行为不一致==Inconsistent behavior on java#39;s ==(java的行为不一致==)
为什么 Java 能够将 0xff000000 存储为 int?Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)