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

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

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

      1. <i id='qBxoe'><tr id='qBxoe'><dt id='qBxoe'><q id='qBxoe'><span id='qBxoe'><b id='qBxoe'><form id='qBxoe'><ins id='qBxoe'></ins><ul id='qBxoe'></ul><sub id='qBxoe'></sub></form><legend id='qBxoe'></legend><bdo id='qBxoe'><pre id='qBxoe'><center id='qBxoe'></center></pre></bdo></b><th id='qBxoe'></th></span></q></dt></tr></i><div id='qBxoe'><tfoot id='qBxoe'></tfoot><dl id='qBxoe'><fieldset id='qBxoe'></fieldset></dl></div>
      2. 使用 PowerMockito 1.6 验证静态方法调用

        时间:2023-09-26

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

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

          1. <tfoot id='fKX69'></tfoot>
              <tbody id='fKX69'></tbody>

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

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

                  问题描述

                  我正在为类似于下面给出的示例的方法编写 JUnit 测试用例:

                  I am writing JUnit test case for methods similar to sample given below:

                  Class SampleA{
                      public static void methodA(){
                          boolean isSuccessful = methodB();
                          if(isSuccessful){
                              SampleB.methodC();
                          }
                      }
                  
                      public static boolean methodB(){
                          //some logic
                          return true;
                      }
                  }
                  
                  Class SampleB{
                      public static void methodC(){
                          return;
                      }
                  }
                  

                  我在我的测试类中编写了以下测试用例:

                  I wrote the following test case in my test class:

                  @Test
                  public void testMethodA_1(){
                      PowerMockito.mockStatic(SampleA.class,SampleB.class);
                  
                      PowerMockito.when(SampleA.methodB()).thenReturn(true);
                      PowerMockito.doNothing().when(SampleB.class,"methodC");
                  
                      PowerMockito.doCallRealMethod().when(SampleA.class,"methodA");
                      SampleA.methodA();
                  }
                  

                  现在我想验证是否调用了 Sample B 类的静态方法 C().如何使用 PowerMockito 1.6 实现?我已经尝试了很多东西,但它似乎对我来说并不奏效.任何帮助表示赞赏.

                  Now I want to verify whether static methodC() of class Sample B is called or not. How can I achieve using PowerMockito 1.6? I have tried many things but it doesn't seems to be working out for me. Any help is appreciated.

                  推荐答案

                  就个人而言,我不得不说 PowerMock 等是解决问题的解决方案,如果你的代码还不错的话,你不应该有.在某些情况下,它是必需的,因为框架等使用静态方法会导致代码根本无法测试,但如果是关于您的代码,您应该始终更喜欢重构而不是静态模拟.

                  Personally, I have to say that PowerMock, etc. is the solution to a problem that you shouldn't have if your code wasn't bad. In some cases, it is required because frameworks, etc. use static methods that lead to code that simply cannot be tested otherwise, but if it's about YOUR code, you should always prefer refactoring instead of static mocking.

                  无论如何,在 PowerMockito 中进行验证应该不会那么难......

                  Anyway, verifing that in PowerMockito shouldn't be that hard...

                  PowerMockito.verifyStatic( Mockito.times(1)); // Verify that the following mock method was called exactly 1 time
                  SampleB.methodC();
                  

                  (当然,要使其工作,您必须将 SampleB 添加到 @PrepareForTest 注释并为其调用 mockStatic.)

                  (Of course, for this to work you must add SampleB to the @PrepareForTest annotation and call mockStatic for it.)

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

                  上一篇:参数匹配器的无效使用 下一篇:异常Junit 4.10后的Mockito验证

                  相关文章

                  最新文章

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

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

                • <tfoot id='PUtlo'></tfoot>

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