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

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

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

        如何通过mockito验证是否从其他具有相同类的方法

        时间:2023-09-25
      1. <tfoot id='motj8'></tfoot>

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

                <bdo id='motj8'></bdo><ul id='motj8'></ul>
                1. <small id='motj8'></small><noframes id='motj8'>

                    <tbody id='motj8'></tbody>
                  本文介绍了如何通过mockito验证是否从其他具有相同类的方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想测试一些在同一个类中调用其他方法的方法.它们基本上是相同的方法,但具有不同数量的参数,因为数据库中有一些默认值.我在这个上显示

                  I want to test some methods that call others in the same class. They are basically the same methods, but with different numbers of arguments because there are some default values in a database. I show on this

                  public class A{
                      Integer quantity;
                      Integer price;        
                  
                      A(Integer q, Integer v){
                          this quantity = q;
                          this.price = p;
                      }
                  
                      public Float getPriceForOne(){
                          return price/quantity;
                      }
                  
                      public Float getPrice(int quantity){
                          return getPriceForOne()*quantity;
                      }
                  }
                  

                  所以我想测试调用getPrice(int)方法时是否调用了getPriceForOne()方法.基本上像平常一样调用 getPrice(int) 并模拟 getPriceForOne.

                  So I want to test if the method getPriceForOne() was called when calling the method getPrice(int). Basically call getPrice(int) as normal and mock getPriceForOne.

                  import static org.mockito.Mockito.times;
                  import static org.mockito.Mockito.verify;
                  ....
                  
                  public class MyTests {
                      A mockedA = createMockA();
                  
                      @Test
                      public void getPriceTest(){
                          A a = new A(3,15);
                          ... test logic of method without mock ...
                  
                          mockedA.getPrice(2);
                          verify(mockedA, times(1)).getPriceForOne();
                      }
                  }
                  

                  请记住,我有一个更复杂的文件,它是其他人的实用程序,它们必须都在一个文件中.

                  Please keep in mind that I have a much more complicated file that's a utility for others and they must all be in one file.

                  推荐答案

                  你需要一个间谍,而不是一个模拟 A:

                  You would need a spy, not a mock A:

                      A a = Mockito.spy(new A(1,1));
                      a.getPrice(2);
                      verify(a, times(1)).getPriceForOne();
                  

                  这篇关于如何通过mockito验证是否从其他具有相同类的方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Mockito:模拟将在 for 循环中循环的数组列表 下一篇:如何模拟影响对象的无效返回方法

                  相关文章

                  最新文章

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

                        <bdo id='iI8Rp'></bdo><ul id='iI8Rp'></ul>
                    1. <small id='iI8Rp'></small><noframes id='iI8Rp'>