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

      • <bdo id='Tr2Oq'></bdo><ul id='Tr2Oq'></ul>
      1. <small id='Tr2Oq'></small><noframes id='Tr2Oq'>

        <tfoot id='Tr2Oq'></tfoot>

        <legend id='Tr2Oq'><style id='Tr2Oq'><dir id='Tr2Oq'><q id='Tr2Oq'></q></dir></style></legend>
      2. Powermock - 模拟一个超级方法调用

        时间:2023-09-25
          • <bdo id='S6zUn'></bdo><ul id='S6zUn'></ul>
              • <small id='S6zUn'></small><noframes id='S6zUn'>

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

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

                  问题描述

                  这是我的代码 -

                  import org.junit.Test;
                  import org.junit.runner.RunWith;
                  import org.powermock.modules.junit4.PowerMockRunner;
                  
                  import org.powermock.core.classloader.annotations.*;
                  import static org.powermock.api.support.SuppressCode.*;
                  
                  class BaseService {
                      public int save() {
                          validate();
                          return 2;
                      }
                  
                      public static int save2() {
                          return 5;
                      }
                  
                      public void validate() {
                          System.out.println("base service save executing...");
                      }
                  }
                  
                  class ChildService extends BaseService {
                      public int save() {
                          System.out.println("child service save executing...");
                          int x = super.save2();
                          int y = super.save();
                          System.out.println("super.save returned " + y);
                          load();
                          return 1 + x;
                      }
                  
                      public void load() {
                          System.out.println("child service load executing...");
                      }
                  }
                  
                  @RunWith(PowerMockRunner.class)
                  @PrepareForTest(BaseService.class)
                  public class PreventSuperInvocation {
                  
                      @Test
                      public void testSave() throws Exception {
                  
                          org.powermock.api.support.Stubber.stubMethod(BaseService.class,
                                  "save2", 4);
                          suppressMethod(BaseService.class, "save");
                          ChildService childService = new ChildService();
                          System.out.println(childService.save());
                      }
                  
                  }
                  

                  我想在 ChildService 类中模拟 super.save().但我找不到这样做的方法.suppressMethod() 只抑制并返回一个默认值(在上述情况下为 0).而 MemberModifierStubberMethodProxy 之类的东西只适用于静态方法.

                  I would like to mock super.save() in ChildService class. But I can't find a way of doing it. suppressMethod() only suppresses and returns a default value (0 in the above case). And things like MemberModifier, Stubber, MethodProxy only work for static methods.

                  有没有办法在 Powermock 中做到这一点?

                  Is there a way of doing this in Powermock?

                  我正在使用 Powermock 1.5 和 Mockito 1.9.5.

                  I'm using Powermock 1.5 and Mockito 1.9.5.

                  推荐答案

                  看来jMockit可以满足我的需求.也许我会将这个问题发布到 powermock 邮件列表.同时下面应该足够了.包 learning_mocking_tools.learning_mocking_tools;包 learning_mocking_tools.learning_mocking_tools;

                  It seems that jMockit can do what I need. Maybe I will I post this question to the powermock mailing list. Meanwhile below should suffice. package learning_mocking_tools.learning_mocking_tools; package learning_mocking_tools.learning_mocking_tools;

                  import mockit.*;
                  
                  import org.junit.Assert;
                  import org.junit.Test;
                  
                  
                  class BaseService {
                      public int save() {
                          validate();
                          return 2;
                      }
                  
                      public static int save2() {
                          return 5;
                      }
                  
                      public void validate() {
                          System.out.println("base service save executing...");
                      }
                  }
                  
                  class ChildService extends BaseService {
                      public int save() {
                          System.out.println("child service save executing...");
                          int x = super.save2();
                          int y = super.save();
                          System.out.println("super.save returned " + y);
                          load();
                          return 1 + y;
                      }
                  
                      public void load() {
                          System.out.println("child service load executing...");
                      }
                  }
                  
                  @MockClass(realClass = BaseService.class)
                  class MockBase {
                  
                      @Mock
                      public int save() {
                          System.out.println("mocked base");
                          return 9;
                      }
                  }
                  
                  public class PreventSuperInvocation {
                  
                      @Test
                      public void testSave() throws Exception {
                          MockBase mockBase = new MockBase();
                          Mockit.setUpMock(BaseService.class, mockBase);
                  
                          ChildService childService = new ChildService();
                  //      int x = childService.save();
                  
                          Assert.assertEquals(9 + 1, childService.save());
                  
                          Mockit.tearDownMocks();
                      }
                  
                  }
                  

                  这篇关于Powermock - 模拟一个超级方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么没有“从不分配字段"?@Mock 警告 下一篇:带有局部变量的 Mockito

                  相关文章

                  最新文章

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

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

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

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