Mockito Guide: Mocking Superclass Methods in JUnit Tests

Introduction

When writing unit tests,

you may encounter situations where a child class calls a method from its superclass, and you want to mock that superclass method. This is useful when the superclass method performs complex logic or external operations that you want to avoid during testing.

Here in this method’s mock example I am going to show you how to mock super class method in Junit test class. A class that extends another class is called a child class and a class that is extended by another class is called a super class. So, basically when a child class calls super class method within its own method then perhaps you may need to mock the super class method. In this case your super class method does not need to be called actually to perform the operations and you want to return from your child class method.

Example Scenario

Child → Parent Inheritance

Parent Child Scaled 2

Superclass: Parent

Let’s say that you have the following display() method in Parent class:

public class Parent {

  public void display() {
	System.out.println("Display in Parent class method display()");
  }
}

Subclass: Child

You have the following Child class method show() that calls the Parent class method display():

public class Child extends Parent {
 
  public void show() {
	 System.out.println("Inside Child's show() method");
	 display();
	 System.out.println("After calling Parent's dispaly() method");
  }
}

In this example, the Child class calls the display() method from its Parent class. If you want to test the show() method without executing the actual logic inside display(), you can mock it using Mockito.

The display() method has void as a return type. So, I will use doNothing() from Mockito framework, because this method does not return anything.

Mocking void Methods with Mockito

Since display() returns void, use Mockito.doNothing() to mock it:

JUnit Test Class

public class ChildTest {





	private Child child = Mockito.spy(new Child());

	@Test
	public void testShow() {
		// Child child = Mockito.spy(new Child());

		// Mockito.doNothing().when((Parent) child).display();
		Mockito.doNothing().when(child).display();





		child.show();

		Mockito.verify(child, Mockito.times(1)).show();
	}

}

Here, Mockito.spy() creates a partial mock of the Child object, and doNothing() ensures that the display() method does nothing when called.

The Mockito.doNothing() will actually do nothing when Parent’s display() method is called. Finally, I have verified that the Child’s show() method has been executed at least once using Mockito.verify() method.

Mocking Methods with Return Values

If the superclass method returns a value, use Mockito.when(...).thenReturn(...).

Superclass Method

Now if you have any method in parent class that returns something from the method. So, in this case also how you want to perform your test, it depends on you. For example, let’s say Parent class has the following method that returns String.

public String getMsg(String name) {
	return "Hello, " + name;
}

Subclass Method

And the Child class has the following method that calls the above getMsg() method.

public String getWelcomeMsg(String name) {
	System.out.println("Inside Child's getWelcomeMsg() method");
	return getMsg(name);
}

It’s up to you whether you want to mock or not the getMsg() method in Junit test class.

JUnit Test

Here is an example of the Junit test for the above Child method getWelcomeMsg():

@Test
public void testGetWelcomeMsg() {
	Mockito.when(child.getMsg(Mockito.anyString())).thenReturn("Soumitra");

	String msg = child.getWelcomeMsg("Roy Tutorials");

	// assertEquals("Hello, Roy Tutorials", msg);

	assertEquals("Soumitra", msg);
}

This test mocks the getMsg() method to return "Soumitra" regardless of the input, allowing you to isolate and test the behavior of getWelcomeMsg().

Notes

  • Use Mockito.spy() for partial mocking.
  • Use doNothing() for mocking void methods.
  • Use when(...).thenReturn(...) for mocking methods with return values.
  • If you need to mock static or final methods, consider using PowerMock, though it’s generally better to design your code for testability.

Output Example

Running the above Junit test class will produce the following output:

Inside Child's show() method
After calling Parent's dispaly() method
Inside Child's getWelcomeMsg() method

Source Code

Download

Conclusion

Mocking superclass methods in JUnit is a powerful technique for isolating and testing subclass behavior without relying on inherited logic. Using Mockito, you can easily mock both void and return-type methods from a parent class, allowing for cleaner, more focused unit tests. Whether you’re dealing with inheritance-heavy code or simply want to avoid side effects from superclass methods, this approach helps improve test reliability and maintainability. By following the examples and best practices outlined in this guide, you can confidently write unit tests that are both robust and efficient.

Share

Related posts

No comments

Leave a comment