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

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

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

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

        如何使用 PowerMockito 模拟私有静态方法?

        时间:2023-09-26

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

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

              <tfoot id='q5Ytf'></tfoot>
            • <legend id='q5Ytf'><style id='q5Ytf'><dir id='q5Ytf'><q id='q5Ytf'></q></dir></style></legend>

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

                    <tbody id='q5Ytf'></tbody>
                  本文介绍了如何使用 PowerMockito 模拟私有静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试模拟私有静态方法 anotherMethod().见下面的代码

                  I'm trying to mock private static method anotherMethod(). See code below

                  public class Util {
                      public static String method(){
                          return anotherMethod();
                      }
                  
                      private static String anotherMethod() {
                          throw new RuntimeException(); // logic was replaced with exception.
                      }
                  }
                  

                  这是我的测试代码

                  @PrepareForTest(Util.class)
                  public class UtilTest extends PowerMockTestCase {
                  
                          @Test
                          public void should_prevent_invoking_of_private_method_but_return_result_of_it() throws Exception {
                  
                              PowerMockito.mockStatic(Util.class);
                              PowerMockito.when(Util.class, "anotherMethod").thenReturn("abc");
                  
                              String retrieved = Util.method();
                  
                              assertNotNull(retrieved);
                              assertEquals(retrieved, "abc");
                          }    
                  }
                  

                  但是我运行的每一个图块都会出现这个异常

                  But every tile I run it I get this exception

                  java.lang.AssertionError: expected object to not be null
                  

                  我想我在嘲笑东西方面做错了.有什么想法可以解决吗?

                  I suppose that I'm doing something wrong with mocking stuff. Any ideas how can I fix it?

                  推荐答案

                  为此,您可以使用 PowerMockito.spy(...)PowerMockito.doReturn(...).

                  To to this, you can use PowerMockito.spy(...) and PowerMockito.doReturn(...).

                  此外,您必须在测试类中指定 PowerMock 运行器,并准备测试类,如下所示:

                  Moreover, you have to specify the PowerMock runner at your test class, and prepare the class for testing, as follows:

                  @PrepareForTest(Util.class)
                  @RunWith(PowerMockRunner.class)
                  public class UtilTest {
                  
                     @Test
                     public void testMethod() throws Exception {
                        PowerMockito.spy(Util.class);
                        PowerMockito.doReturn("abc").when(Util.class, "anotherMethod");
                  
                        String retrieved = Util.method();
                  
                        Assert.assertNotNull(retrieved);
                        Assert.assertEquals(retrieved, "abc");
                     }
                  }
                  

                  希望对你有帮助.

                  这篇关于如何使用 PowerMockito 模拟私有静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:是否可以在 PowerMock 中对私有静态方法使用部分模 下一篇:参数匹配器的无效使用

                  相关文章

                  最新文章

                1. <tfoot id='vx2vA'></tfoot>

                      <legend id='vx2vA'><style id='vx2vA'><dir id='vx2vA'><q id='vx2vA'></q></dir></style></legend>
                      • <bdo id='vx2vA'></bdo><ul id='vx2vA'></ul>

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

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