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

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

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

      如何使用 Mockito 或任何其他相关的 Java 框架来模

      时间:2023-09-25
      1. <legend id='9seHq'><style id='9seHq'><dir id='9seHq'><q id='9seHq'></q></dir></style></legend>
            • <bdo id='9seHq'></bdo><ul id='9seHq'></ul>
              <tfoot id='9seHq'></tfoot>
            • <i id='9seHq'><tr id='9seHq'><dt id='9seHq'><q id='9seHq'><span id='9seHq'><b id='9seHq'><form id='9seHq'><ins id='9seHq'></ins><ul id='9seHq'></ul><sub id='9seHq'></sub></form><legend id='9seHq'></legend><bdo id='9seHq'><pre id='9seHq'><center id='9seHq'></center></pre></bdo></b><th id='9seHq'></th></span></q></dt></tr></i><div id='9seHq'><tfoot id='9seHq'></tfoot><dl id='9seHq'><fieldset id='9seHq'></fieldset></dl></div>
                <tbody id='9seHq'></tbody>

                <small id='9seHq'></small><noframes id='9seHq'>

                本文介绍了如何使用 Mockito 或任何其他相关的 Java 框架来模拟超类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我的场景如下

                class SuperClass{
                   public void run(){
                      System.out.println("I am running in Super class");
                   }
                }
                
                class ChildClass extends SuperClass{
                  public void childRunner(){
                     System.out.println("Step 1");
                     System.out.println("Step 2");
                     **run();**
                     System.out.println("Last Step");
                  }
                }
                

                现在我想模拟 ChildClasschildRunner() 方法,因为这个方法在内部调用超类方法,我需要一些帮助/一段代码来说明如何模拟 ChildClasschildRunner() 方法中存在的 run() 方法.

                Now I want to mock the childRunner() method of ChildClass and since this method internally calls the super class method, i need some help/piece of code on how to mock the run() method which is present in the childRunner() method of ChildClass.

                推荐答案

                理想情况下,您应该优先考虑组合而不是继承".

                Ideally, you should "favor composition over inheritance".

                如果你没有这个选项,你可以使用 doNothing,它基本上告诉 Mockito 在调用 mock/spy 对象中的方法时什么都不做.这也讨论过这里

                If you don't have this option you could use doNothing which basically tells Mockito to do nothing when a method in a mock/spy object is called. This was also discussed here

                以下代码示例应该会有所帮助

                Following code example should help

                @Test
                public void tst() {
                    ChildClass ch = Mockito.spy(new ChildClass());
                    Mockito.doNothing().when((SuperClass)ch).run();
                    ch.childRunner();
                
                }
                
                class SuperClass{
                    public void run(){
                        System.out.println("I am running in Super class");
                    }
                }
                
                class ChildClass extends SuperClass{
                    public void childRunner(){
                        System.out.println("Step 1");
                        run();
                        System.out.println("Last Step");
                    }
                }
                

                输出:

                Step 1
                Last Step
                

                如果你使用 super.run();这行不通

                In case you use super.run(); this won't work

                这篇关于如何使用 Mockito 或任何其他相关的 Java 框架来模拟超类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 AndroidTestCase 中调用 MockitoAnnotations.initMocks() 时 下一篇:测试是否调用了另一个方法

                相关文章

                最新文章

              1. <tfoot id='88U2r'></tfoot>

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

                    <small id='88U2r'></small><noframes id='88U2r'>