1. <small id='RtUah'></small><noframes id='RtUah'>

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

        PowerMock,模拟一个静态方法,然后在所有其他静

        时间:2023-09-26
            <tbody id='l2jSA'></tbody>

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

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

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

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

                  本文介绍了PowerMock,模拟一个静态方法,然后在所有其他静态上调用真实方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在设置模拟类的静态方法.我必须在 @Before 注释的 JUnit 设置方法中执行此操作.

                  I'm setting up mocking a class' static methods. I have to do this in a @Before-annotated JUnit setup method.

                  我的目标是设置类来调用真正的方法,除了那些我明确模拟的方法.

                  My goal is to setup the class to call real methods, except for those methods I explicitly mock.

                  基本上:

                  @Before
                  public void setupStaticUtil() {
                    PowerMockito.mockStatic(StaticUtilClass.class);
                  
                    // mock out certain methods...
                    when(StaticUtilClass.someStaticMethod(anyString())).thenReturn(5); 
                  
                    // Now have all OTHER methods call the real implementation???  How do I do this?
                  }
                  

                  我遇到的问题是在 StaticUtilClass 方法 public static int someStaticMethod(String s) 不幸地抛出了一个 RuntimeException 如果提供 null 值.

                  The problem I'm running into is that within StaticUtilClass the method public static int someStaticMethod(String s) unfortunately throws a RuntimeException if supplied with a null value.

                  所以我不能简单地将调用真实方法作为默认答案的明显路线如下:

                  So I can't simply go the obvious route of calling real methods as the default answer as below:

                  @Before
                  public void setupStaticUtil() {
                    PowerMockito.mockStatic(StaticUtilClass.class, CALLS_REAL_METHODS); // Default to calling real static methods
                  
                    // The below call to someStaticMethod() will throw a RuntimeException, as the arg is null!
                    // Even though I don't actually want to call the method, I just want to setup a mock result
                    when(StaticUtilClass.someStaticMethod(antString())).thenReturn(5); 
                  }
                  

                  我需要设置默认答案以在所有其他静态方法上调用真实方法之后我模拟了我感兴趣的方法的结果.

                  I need to set the default Answer to call real methods on all other static methods after I mock the results from the method I'm interested in mocking.

                  这可能吗?

                  推荐答案

                  你在找什么叫做partial mocking.

                  在 PowerMock 中你可以使用 mockStaticPartial 方法.

                  In PowerMock you can use mockStaticPartial method.

                  在 PowerMockito 中,您可以使用存根,它只会存根定义的方法,而其他保持不变:

                  In PowerMockito you can use stubbing, which will stub only the method defined and leave other unchanged:

                  PowerMockito.stub(PowerMockito.method(StaticUtilClass.class, "someStaticMethod")).toReturn(5);
                  

                  也别忘了

                  @PrepareForTest(StaticUtilClass.class)
                  

                  这篇关于PowerMock,模拟一个静态方法,然后在所有其他静态上调用真实方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Mockito.any() 通过泛型接口 下一篇:Mockito Matchers isA、any、eq 和 same 有什么区别?

                  相关文章

                  最新文章

                    <bdo id='wrrSz'></bdo><ul id='wrrSz'></ul>
                  <legend id='wrrSz'><style id='wrrSz'><dir id='wrrSz'><q id='wrrSz'></q></dir></style></legend>
                  1. <small id='wrrSz'></small><noframes id='wrrSz'>

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