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

      <small id='8Ev0O'></small><noframes id='8Ev0O'>

        <legend id='8Ev0O'><style id='8Ev0O'><dir id='8Ev0O'><q id='8Ev0O'></q></dir></style></legend>

        如何模拟 void 静态方法以使用 Powermock 引发异常

        时间:2023-09-26

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

                  <tbody id='4yTRK'></tbody>
                <legend id='4yTRK'><style id='4yTRK'><dir id='4yTRK'><q id='4yTRK'></q></dir></style></legend>

                <small id='4yTRK'></small><noframes id='4yTRK'>

                  本文介绍了如何模拟 void 静态方法以使用 Powermock 引发异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Powermock 和 Mockito 来模拟 void 静态方法来抛出异常,如下所示.但是我遇到了一个问题.除非我使用相同的参数对 Adder.add() 进行两次调用,否则不会抛出模拟的 IOException.

                  I am trying to use Powermock and Mockito to mock a void static method to throw exception as below. But I met a problem. Unless I make the two invocations of Adder.add() with the same argument, the mocked IOException won't be thrown.

                  顺便说一句,我已将 @RunWith(PowerMockRunner.class)@PrepareForTest(Adder.class) 添加到单元测试类中.

                  BTW, I've added @RunWith(PowerMockRunner.class) and @PrepareForTest(Adder.class) to the unit test class.

                  class Adder{
                      public static void add(int i) throws IOException{
                          return;
                      }
                  }
                  
                  @Test
                  public void testAdder() throws IOException{
                      PowerMockito.mockStatic(Adder.class);
                      PowerMockito.doThrow(new IOException()).when(Adder.class);
                      Adder.add(12);
                      try {
                          Adder.add(11);
                      } catch (Exception e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                      // assert things 
                  }
                  

                  提前致谢.:)

                  答案如下.

                  在这里咨询后http://code.google.com/p/powermock/issues/detail?id=278 ,其实上面的 Adder.add(12) 就是设置 mock 静态方法的一部分.这意味着当使用参数 12 调用 Adder.add() 时,将抛出 IOException.这很难理解,对吧?:) 所以应该写成下面这样.

                  After consulting here http://code.google.com/p/powermock/issues/detail?id=278 , in fact Adder.add(12) above is part of setting up mock static method. It means when invoking Adder.add() with argument 12, IOException will be thrown. It is hard to understand, right? :) So it should be written as below.

                  PowerMockito.mockStatic(Adder.class);
                  PowerMockito.doThrow(new IOException()).when(Adder.class);
                  Adder.add(anyInt());
                  

                  推荐答案

                  答案如下.

                  在这里咨询后http://code.google.com/p/powermock/issues/detail?id=278 ,其实上面的 Adder.add(12) 就是设置 mock 静态方法的一部分.这意味着当使用参数 12 调用 Adder.add() 时,将抛出 IOException.这很难理解,对吧?:) 所以应该写成下面这样.

                  After consulting here http://code.google.com/p/powermock/issues/detail?id=278 , in fact Adder.add(12) above is part of setting up mock static method. It means when invoking Adder.add() with argument 12, IOException will be thrown. It is hard to understand, right? :) So it should be written as below.

                  PowerMockito.mockStatic(Adder.class);
                  PowerMockito.doThrow(new IOException()).when(Adder.class);
                  Adder.add(anyInt());
                  


                  链接已失效,请尝试 Internet Archive 一个.

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

                  上一篇:使用 PowerMock 模拟私有方法,但仍会调用底层方法 下一篇:为什么在 Java 中使用静态辅助方法不好?

                  相关文章

                  最新文章

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

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

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