Java 如何验证父类的 super.method() 调用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24896636/
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-14 15:25:58  来源:igfitidea点击:

How to verify invocation of super.method() of parent class?

javaunit-testingmockitopowermock

提问by Aaron

I have three very simple classes. One of them extends parent class.

我有三个非常简单的课程。其中之一扩展了父类。

public class Parent{
    protected String print() {
        // some code
    }
}

Here is a child class.

这里是儿童班。

public class Child extends Parent {
    /**
     * Shouldn't invoke protected Parent.print() of parent class.
     */
    @Override
    protected String print() {
        // some additional behavior
        return super.print();
    }
}

And test class.

和考试班。

public class ChildTest {

    @Test
    public void should_mock_invocation_of_protected_method_of_parent_class() throws Exception {

        // Given
        Child child = PowerMockito.mock(Child.class);
        Method method = PowerMockito.method(Parent.class, "print");
        PowerMockito.when(child, method).withNoArguments().thenReturn("abc");

        // When
        String retrieved = child.print();

        // Than
        Mockito.verify(child, times(1)).print(); // verification of child method
        Assert.assertEquals(retrieved, "abc");
    }
}

I need to verify super.print()invocation. How can I do it?

我需要验证super.print()调用。我该怎么做?

回答by Eran

If you call super.print()in the Child class' print()method, of course the Parent class implementation of print()will be called. How to verify that it actually happens depends on what the Parent class implementation actually does.

如果你super.print()在 Child 类的print()方法中调用,当然print()会调用Parent 类的实现。如何验证它确实发生了取决于 Parent 类实现实际上做了什么。

P.S. the comments in your code Shouldn't invoke protected Parent.print() of parent classand Parent.print() method shouldn't be invoked.are wrong.

PS在你的代码中的注释Shouldn't invoke protected Parent.print() of parent classParent.print() method shouldn't be invoked.是错误的。

回答by zardosht

I think you want to verify in your test that the print()implementation of parent class is called from child class.

我想你想在你的测试中验证print()父类的实现是从子类调用的。

This does not require verification. The super.someMethod()is called according to java language specification.

这不需要验证。在super.someMethod()根据所谓的Java语言规范

回答by Geof

This is a long time ago question, but here is how i did it, using Mockito spy, create a method which call the parent method in the child class:

这是很久以前的问题,但这里是我是如何做到的,使用 Mockito 间谍,创建一个方法来调用子类中的父方法:

public class Child extends Parent {
    /**
     * Shouldn't invoke protected Parent.print() of parent class.
     */
    @Override
    protected String print() {
        // some additional behavior
    return callParent();
    }

    protected callParent()
    {
      super.print();
    }

}

And in the test :

在测试中:

@Test
public void sould_mock_invocation_of_protected_method_of_parent_class() throws Exception {

    // Given
    Child child = Mockito.spy(new Child());
    Mockito.doReturn(null)
           .when(child)
           .callParent();
    // When
    String retrieved = child.print();

    // Then
    Mockito.verify(child, times(1)).callParent(); // verification of child method
    Assert.assertEquals(retrieved, "abc");
}

Note: this test only check we call the parent method in the child class

注意:这个测试只检查我们在子类中调用父方法