How To Mock Super Class Method In Junit

Table of Contents

Introduction

Here in this 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.

junit mock super class method

Example

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

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

Now you may not want to perform the actual business logic whatever there inside the display() method, so in this case you need to mock the display() method. But if you want to perform the business for display() method then you don’t need to mock the display() method.

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

Prerequisites

Java 1.8+, Junit 4.11 – 4.13.2, Powermock 2.0.9

Junit Test Class

The following Junit class will test the above scenario.

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

}

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.

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

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.

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

When you mock the getMsg() method then it will return the mocked value from getWelcomeMsg() otherwise it will return the actual value.

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

Hope you got an idea how to mock parent class method using Junit.

Source Code

Download

Leave a Reply

Your email address will not be published.