How to check the call to the super.method () class of the parent class?

I have three very simple classes. One of them extends the 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 a 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 check the call to super.print() . How can i do this?

+12
source share
4 answers

This is an old question, but here is how I did it using the Mockito spy, creating a method that calls the parent method in the child class:

 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 checks that we call the parent method in the child class.

+2
source

Since you are requesting unit test, I offer you the unit test print() method of the Parent class, not the Child class.

Further, since the print() method of the Child class does not add any behavior, it can be removed:

 public class Child extends Parent { // inherits print() method from Parent } 

This contract is part of the Java Language Specification (JLS) and does not need to be verified:

Class C inherits from its direct superclass and direct superinterfaces all abstract and non-abstract methods of the superclass and superinterfaces that are public, protected or declared by default in the same package as C, and are not overridden (ยง 8.4.8.1) and not hidden (ยง8.4.8.2) by a declaration in the class.

0
source

If you call super.print() in the class method Child < print() , of course, the implementation of the parent class print() will be called. How to check if this is really happening depends on what the parent class implementation really does.

PS comments in your code Shouldn't invoke protected Parent.print() of parent class and Parent.print() method shouldn't be invoked. wrong.

-1
source

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

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

-1
source

Source: https://habr.com/ru/post/972669/


All Articles