<bdo id='uqsKn'></bdo><ul id='uqsKn'></ul>
<i id='uqsKn'><tr id='uqsKn'><dt id='uqsKn'><q id='uqsKn'><span id='uqsKn'><b id='uqsKn'><form id='uqsKn'><ins id='uqsKn'></ins><ul id='uqsKn'></ul><sub id='uqsKn'></sub></form><legend id='uqsKn'></legend><bdo id='uqsKn'><pre id='uqsKn'><center id='uqsKn'></center></pre></bdo></b><th id='uqsKn'></th></span></q></dt></tr></i><div id='uqsKn'><tfoot id='uqsKn'></tfoot><dl id='uqsKn'><fieldset id='uqsKn'></fieldset></dl></div>

<small id='uqsKn'></small><noframes id='uqsKn'>

    <legend id='uqsKn'><style id='uqsKn'><dir id='uqsKn'><q id='uqsKn'></q></dir></style></legend>
  1. <tfoot id='uqsKn'></tfoot>
    1. Mockito - 如何验证从未调用过模拟

      时间:2023-09-25
        <tbody id='PZN1k'></tbody>

    2. <i id='PZN1k'><tr id='PZN1k'><dt id='PZN1k'><q id='PZN1k'><span id='PZN1k'><b id='PZN1k'><form id='PZN1k'><ins id='PZN1k'></ins><ul id='PZN1k'></ul><sub id='PZN1k'></sub></form><legend id='PZN1k'></legend><bdo id='PZN1k'><pre id='PZN1k'><center id='PZN1k'></center></pre></bdo></b><th id='PZN1k'></th></span></q></dt></tr></i><div id='PZN1k'><tfoot id='PZN1k'></tfoot><dl id='PZN1k'><fieldset id='PZN1k'></fieldset></dl></div>

      <tfoot id='PZN1k'></tfoot>
        • <bdo id='PZN1k'></bdo><ul id='PZN1k'></ul>

            <small id='PZN1k'></small><noframes id='PZN1k'>

          • <legend id='PZN1k'><style id='PZN1k'><dir id='PZN1k'><q id='PZN1k'></q></dir></style></legend>

              1. 本文介绍了Mockito - 如何验证从未调用过模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在寻找一种使用 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模板网!

                上一篇:使用 Mockito 将模拟对象注入在测试中声明为字段 下一篇:如何使用 Mockito 在模拟上显示所有调用

                相关文章

                最新文章

              2. <legend id='KMW0Y'><style id='KMW0Y'><dir id='KMW0Y'><q id='KMW0Y'></q></dir></style></legend>
              3. <small id='KMW0Y'></small><noframes id='KMW0Y'>

                    <bdo id='KMW0Y'></bdo><ul id='KMW0Y'></ul>
                1. <tfoot id='KMW0Y'></tfoot>

                  <i id='KMW0Y'><tr id='KMW0Y'><dt id='KMW0Y'><q id='KMW0Y'><span id='KMW0Y'><b id='KMW0Y'><form id='KMW0Y'><ins id='KMW0Y'></ins><ul id='KMW0Y'></ul><sub id='KMW0Y'></sub></form><legend id='KMW0Y'></legend><bdo id='KMW0Y'><pre id='KMW0Y'><center id='KMW0Y'></center></pre></bdo></b><th id='KMW0Y'></th></span></q></dt></tr></i><div id='KMW0Y'><tfoot id='KMW0Y'></tfoot><dl id='KMW0Y'><fieldset id='KMW0Y'></fieldset></dl></div>