[JAVA] How to mock a super method call in PowerMock


In JUnit using Mockito and Powermock, I had to mock the processing of the parent class using super, so I made a note of it. If you look it up, you can use PowerMockito.suppress () quite a bit, but if you use suppress (), it will only be skipped, and you should return the mock object you expected. I was in a state where I couldn't execute the subsequent processing of JUnit successfully.

Tested class

The sample code is shown below. The DTO class is omitted.

Parent class

public class Parent {

	public OutputDto execute (InputDto input) {
		OutputDto out = new OutputDto();
		return out;

Child class

public class Child extends Parent {

	public OutputDto execute (InputDto input) {
		OutputDto out = super.execute(input);
		out.setText(out.getText()+" updated in Child.");
		return out;

The point is that in the child class, super.execute () is done and the return value is expected.

JUnit test class

@PrepareForTest({Parent.class})//Specify parent
public class MockSuperTest {

	//Tested class
	private Child child;

	public void setUp() throws Exception {

	public void testExecute1() {
		//Input data preparation
		InputDto input = new InputDto();
		OutputDto result = new OutputDto();
		result.setText("hello updated in Child.");

		try {
			Method method = child.getClass().getMethod("execute", InputDto.class);
				new InvocationHandler() {
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						System.out.println("replaced execute()!!");
						return result;
		} catch (Exception e) {

		OutputDto testOut = child.execute(input);
		assertEquals(result.getText(), testOut.getText());

Execution result

When you run JUnit above,

replaced execute()!!

Then, after calling ╩╗execute ()of the child class,super.execute ()is replaced with the content specified byPowerMockito.replace ()`.

By the way,

@PrepareForTest({Child.class})//Specify a child
public class MockSuperTest {

If you specify the place in the child class, the result will be

replaced execute()!!

Directly change ╩╗execute ()` of the neighboring child class. Of course if you run it normally


Is the expected value.

that's all.

Recommended Posts

How to mock a super method call in PowerMock
How to test a private method in Java and partially mock that method
How to create a method
Call the super method in Java
How to insert a video in Rails
How to have params in link_to method
How to store the information entered in textarea in a variable in the method
How to publish a library in jCenter
How to reference a column when overriding the column name method in ActiveRecord
How to mock each case with PowerMock + Mockito1x
How to display a web page in Java
How to add a classpath in Spring Boot
How to create a theme in Liferay 7 / DXP
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to make a follow function in Rails
How to automatically generate a constructor in Eclipse
How to call AmazonSQSAsync
How to make a judgment method to search for an arbitrary character in an array
How to call functions in bulk with Java reflection
How to create a Java environment in just 3 seconds
I want to call a method of another class
How to implement a like feature in Ajax in Rails
How to create a Spring Boot project in IntelliJ
How to create a data URI (base64) in Java
How to launch another command in a Ruby program
How to display a browser preview in VS Code
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
How to store Rakuten API data in a table
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to load JavaScript in a specific view
How to write a core mod in Minecraft Forge 1.15.2
[Ruby/Rails] How to generate a password in a regular expression
How to leave a comment
How to insert a video
How to get the class name / method name running in Java
How to use the getter / setter method (in object orientation)
How to change a string in an array to a number in Ruby
How to create a placeholder part to use in the IN clause
I want to call a method and count the number
How to store a string from ArrayList to String in Java (Personal)
Create a method to return the tax rate in Java
How to call and use API in Java (Spring Boot)
How to test a private method with RSpec for yourself
How to select a specified date by code in FSCalendar
How to display a graph in Ruby on Rails (LazyHighChart)
How to add the same Indexes in a nested array
Mapping to a class with a value object in How to MyBatis
How to develop and register a Sota app in Java
Super easy in 2 steps! How to install devise! !! (rails 5 version)
How to simulate uploading a post-object form to OSS in Java
How to create a service builder portlet in Liferay 7 / DXP
How to set up a proxy with authentication in Feign
How to use the link_to method
How to use Lombok in Spring
How to find May'n in XPath
How to use the include? method
How to add columns to a table