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

        <bdo id='HRZrf'></bdo><ul id='HRZrf'></ul>
    2. <tfoot id='HRZrf'></tfoot>

        模拟受保护的方法

        时间:2023-09-26

        <tfoot id='oVL8n'></tfoot>
      1. <small id='oVL8n'></small><noframes id='oVL8n'>

        <legend id='oVL8n'><style id='oVL8n'><dir id='oVL8n'><q id='oVL8n'></q></dir></style></legend>

                  <tbody id='oVL8n'></tbody>
                  <bdo id='oVL8n'></bdo><ul id='oVL8n'></ul>
                  <i id='oVL8n'><tr id='oVL8n'><dt id='oVL8n'><q id='oVL8n'><span id='oVL8n'><b id='oVL8n'><form id='oVL8n'><ins id='oVL8n'></ins><ul id='oVL8n'></ul><sub id='oVL8n'></sub></form><legend id='oVL8n'></legend><bdo id='oVL8n'><pre id='oVL8n'><center id='oVL8n'></center></pre></bdo></b><th id='oVL8n'></th></span></q></dt></tr></i><div id='oVL8n'><tfoot id='oVL8n'></tfoot><dl id='oVL8n'><fieldset id='oVL8n'></fieldset></dl></div>
                • 本文介绍了模拟受保护的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想模拟一个继承的受保护方法.我不能直接从java代码调用这个方法,因为它是从另一个包中的类继承的.我找不到在 when(...)

                  I want to mock an inherited protected method. I can't call this method directly from java code as it is inherited from class that in another package. I can't find a way to specify this method to stub in in when(...)

                  package a;
                  
                  public class A() {
                      protected int m() {}
                  }
                  
                  package b;
                  
                  public class B extends a.A {
                      // this class currently does not override m method from a.A
                      public asd() {}
                  }
                  
                  // test
                  package b;
                  
                  class BTest {
                      @Test
                      public void testClass() {
                          B instance = PowerMockito.spy(new B());
                          PowerMockito.when(instance, <specify a method m>).thenReturn(123);
                          //PowerMockito.when(instance.m()).thenReturn(123); -- obviously does not work
                      }
                  }
                  

                  我查看了 PowerMockito.when 覆盖,这似乎都只用于私有方法!

                  I looked at PowerMockito.when overrides and this seems that they are all for private methods only!

                  如何指定受保护的方法?

                  How to specify protected method?

                  推荐答案

                  简而言之:不能总是使用 when 来存根间谍;使用 doReturn.

                  Nutshell: Can't always use when to stub spies; use doReturn.

                  假设 spydoReturn(都是 PowerMockito)的静态导入:

                  Assuming static imports of spy and doReturn (both PowerMockito):

                  @RunWith(PowerMockRunner.class)
                  @PrepareForTest(B.class)
                  public class BTest {
                      @Test public void testClass() throws Exception {
                          B b = spy(new B());
                          doReturn(42).when(b, "m");
                          b.asd();
                      }
                  }
                  

                  您也可以@PrepareForTest(A.class) 并在when(a, "m") 上设置doReturn.哪个更有意义取决于实际测试.

                  You could also @PrepareForTest(A.class) and set up the doReturn on when(a, "m"). Which makes more sense depends on the actual test.

                  这篇关于模拟受保护的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Mockito:使用泛型参数进行验证 下一篇:如何使用 PowerMockito 模拟构造函数

                  相关文章

                  最新文章

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

                      <bdo id='QGNWa'></bdo><ul id='QGNWa'></ul>

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

                    1. <legend id='QGNWa'><style id='QGNWa'><dir id='QGNWa'><q id='QGNWa'></q></dir></style></legend>
                    2. <tfoot id='QGNWa'></tfoot>