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

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

      <tfoot id='M1Owe'></tfoot>
        <bdo id='M1Owe'></bdo><ul id='M1Owe'></ul>
        <legend id='M1Owe'><style id='M1Owe'><dir id='M1Owe'><q id='M1Owe'></q></dir></style></legend>

        在我的情况下,Mockito 验证一个函数被调用一次

        时间:2023-09-25

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

          <small id='5GiAc'></small><noframes id='5GiAc'>

                <tbody id='5GiAc'></tbody>

                • <bdo id='5GiAc'></bdo><ul id='5GiAc'></ul>
                  <tfoot id='5GiAc'></tfoot>
                • 本文介绍了在我的情况下,Mockito 验证一个函数被调用一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 Mockito 来编写我的测试用例.我有一个简单的类,其中包含一个我有兴趣测试的函数 countPerson(boolean):

                  I am using Mockito to write my test case. I have a simple class which contains a function countPerson(boolean) which I am interested to test:

                  public class School {
                    //School is a singleton class.
                  
                    public void countPerson(boolean includeTeacher) {
                         if (includeTeacher) {
                            countIncludeTeacher();
                            return;
                         }
                         countOnlyStudents();
                    }
                  
                    public void countIncludeTeacher() {...}
                    public void countOnlyStudents() {...}
                  }
                  

                  在我的单元测试中,我想测试 countPerson(boolean) 函数:

                  In my unit test, I want to test the countPerson(boolean) function:

                  public class SchoolTest{
                     private School mSchool;
                     @Before
                     public void setUp(){
                        mSchool = School.getInstance();
                     }
                     @Test 
                     public void testCountPerson() {
                         mSchool.countPerson(true);
                         //How to test/verify countIncludeTeacher() is invoked once?
                     }
                  }
                  

                  如何使用 Mockito 检查/验证 countIncludeTeacher() 在我的测试用例中被调用一次?

                  How to use Mockito to check/verify countIncludeTeacher() is invoked once in my test case?

                  推荐答案

                  你必须使用 间谍.这里的问题是您想验证一个方法是在 real 对象上调用的,而不是在模拟对象上调用的.你不能在这里使用模拟,因为它会存根类中的所有方法,因此也存根 countPerson 默认不做任何事情.

                  You will have to use a spy. The problem here is that you want to verify that a method was called on a real object, not on a mock. You can't use a mock here, since it will stub all the methods in the class, thereby stubbing also countPerson to do nothing by default.

                  @Test 
                  public void testCountPerson() {
                      School school = School.getInstance();
                      School spySchool = Mockito.spy(school);
                      spySchool.countPerson(true);
                      verify(spySchool).countIncludeTeacher();
                  }
                  

                  但是,请注意,在使用间谍时应该非常小心,因为除非被存根,否则真正的方法是被调用的.引用 Mockito Javadoc:

                  However, note that you should be very careful when using spies because, unless stubbed, the real methods are gettings called. Quoting Mockito Javadoc:

                  应该小心偶尔使用真正的间谍,例如在处理遗留代码时.

                  Real spies should be used carefully and occasionally, for example when dealing with legacy code.

                  这篇关于在我的情况下,Mockito 验证一个函数被调用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:验证是否调用了所有 getter 方法 下一篇:Java中的Mockito“在此处检测到错误的参数"

                  相关文章

                  最新文章

                • <small id='L2NHV'></small><noframes id='L2NHV'>

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