如何使用 Mockito 或任何其他相关的 Java 框架模拟超类方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30319580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:26:51  来源:igfitidea点击:

How to mock super class method using Mockito or anyother relavent java framework

javajunitmockingmockitopowermock

提问by Ravi Kiran Y

My Scenario is as below

我的场景如下

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");
  }
}

Now I want to mock the childRunner()method of ChildClassand 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.

现在我想嘲弄childRunner()的方法ChildClass,并因为这种方法在内部调用父类的方法,我需要关于如何嘲笑一些帮助/件的代码run()是存在于方法childRunner()的方法ChildClass

回答by user1121883

Ideally, you should "favor composition over inheritance".

理想情况下,您应该“更喜欢组合而不是继承”。

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

如果你没有这个选项,你可以使用doNothingwhich 基本上告诉 Mockito 在调用模拟/间谍对象中的方法时什么都不做。这也在这里讨论

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");
    }
}

output:

输出:

Step 1
Last Step

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

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

回答by cazador

Here is an example for a class that extends another class and it has some other dependencies. In this case, I'll move the superclass call into the other method and then mock the superclass caller method.

这是一个扩展另一个类的类的示例,它有一些其他依赖项。在这种情况下,我会将超类调用移动到另一个方法中,然后模拟超类调用方方法。

class Child extends Parent {

  @Autowired
  private Dependicy d;

  public Authentication authenticate(Authentication auth) {
    the code to be tested...
    superAuthenticate(auth);// the code that I don't want to deal with it.
    return auth;
  }

  protected Authentication superAuthenticate(Authentication auth) {
    return super.authenticate(auth);
  }
}

As you can see above, the authenticate method does some logic and then call the super class's method, so I want to mock the superclass call and test my own code block. Here is my test class:

如上所示,authenticate 方法做了一些逻辑,然后调用超类的方法,所以我想模拟超类调用并测试我自己的代码块。这是我的测试课:

@RunWith(MockitoJUnitRunner.class)
public class ChildTest {
    @Mock
    private Dependicy d;
    @InjectMocks
    private Child child = new Child();

    @Test
    public void testSomething() {
        Child spy = Mockito.spy(child);

        when(d.aMethod(aParam)).thenReturn(aResult);
        doReturn(usefulResult).when(spy).superAuthenticate(any());

        Authentication result = spy.authenticate(auth);
        assertThat(result).isNotNull;
    }
}