<small id='1NiOn'></small><noframes id='1NiOn'>

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

          <bdo id='1NiOn'></bdo><ul id='1NiOn'></ul>
      1. <legend id='1NiOn'><style id='1NiOn'><dir id='1NiOn'><q id='1NiOn'></q></dir></style></legend>

        使用 PowerMockito.whenNew() 不会被嘲笑,而是调用原

        时间:2023-09-26
          <bdo id='tjhHZ'></bdo><ul id='tjhHZ'></ul>
          <legend id='tjhHZ'><style id='tjhHZ'><dir id='tjhHZ'><q id='tjhHZ'></q></dir></style></legend>
          • <tfoot id='tjhHZ'></tfoot>

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

            1. <i id='tjhHZ'><tr id='tjhHZ'><dt id='tjhHZ'><q id='tjhHZ'><span id='tjhHZ'><b id='tjhHZ'><form id='tjhHZ'><ins id='tjhHZ'></ins><ul id='tjhHZ'></ul><sub id='tjhHZ'></sub></form><legend id='tjhHZ'></legend><bdo id='tjhHZ'><pre id='tjhHZ'><center id='tjhHZ'></center></pre></bdo></b><th id='tjhHZ'></th></span></q></dt></tr></i><div id='tjhHZ'><tfoot id='tjhHZ'></tfoot><dl id='tjhHZ'><fieldset id='tjhHZ'></fieldset></dl></div>
                  <tbody id='tjhHZ'></tbody>
                1. 本文介绍了使用 PowerMockito.whenNew() 不会被嘲笑,而是调用原始方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个类似下面的代码:

                  I have a code somewhat like this below:

                  Class A {
                    public boolean myMethod(someargs) {
                      MyQueryClass query = new MyQueryClass();
                      Long id = query.getNextId();
                      // some more code
                    }
                  }
                  Class MyQueryClass     {
                    ....
                    public Long getNextId() {
                      //lot of DB code, execute some DB query
                      return id;
                    }
                  }
                  

                  现在我正在为 A.myMethod(someargs) 编写测试.我想跳过真正的方法 query.getNextId() 而是返回一个存根值.基本上,我想模拟 MyQueryClass.

                  Now I'am writing a test for A.myMethod(someargs). I want to skip the real method query.getNextId() and instead return a stub value. Basically, I want to mock MyQueryClass.

                  所以在我的测试用例中,我使用了:

                  So in my test case, I have used:

                  MyQueryClass query = PowerMockito.mock(MyQueryClass.class);
                  PowerMockito.whenNew(MyQueryClass.class).withNoArguments().thenReturn(query);
                  when(query.getNextId()).thenReturn(1000000L);
                  
                  boolean b = A.getInstance().myMethod(args);
                  
                  //asserts
                  

                  我在测试类的开头使用了 @RunWith(PowerMockRunner.class)@PrepareForTest({MyQueryClass.class}).

                  I used @RunWith(PowerMockRunner.class) and @PrepareForTest({MyQueryClass.class}) in the beginning of my test class.

                  但是我调试测试的时候,还是调用了MyQueryClass类的真实方法getNextId().

                  But when I debug the test, it is still calling the real method getNextId() of the MyQueryClass class.

                  我在这里缺少什么?任何人都可以提供帮助,因为我是 Mockito 和 PowerMockito 的新手.

                  What am I missing here? Can anyone help as I am new to Mockito and PowerMockito.

                  推荐答案

                  需要将调用构造函数的类放到@PrepareForTest注解中,而不是正在构造的类 - 请参阅 模拟新对象的构造.

                  You need to put the class where the constructor is called into the @PrepareForTest annotation instead of the class which is being constructed - see Mock construction of new objects.

                  在你的情况下:

                  @PrepareForTest(MyQueryClass.class)

                  @PrepareForTest(A.class)

                  更笼统的:

                  @PrepareForTest(NewInstanceClass.class)

                  @PrepareForTest(ClassThatCreatesTheNewInstance.class)

                  这篇关于使用 PowerMockito.whenNew() 不会被嘲笑,而是调用原始方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Mockito:试图监视方法正在调用原始方法 下一篇:当我们无法将模拟对象传递给类的实例时如何使

                  相关文章

                  最新文章

                  <tfoot id='Kwey9'></tfoot>
                2. <small id='Kwey9'></small><noframes id='Kwey9'>

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