[JAVA] Mock the constructor with PowerMock

table of contents

  1. Procedure for building an environment for using PowerMock
  2. Mock and Spy in PowerMock
  3. Mock static methods with PowerMock
  4. Mock the constructor with PowerMock ← Now here
  5. Mock private methods with PowerMock
  6. Try using WhiteBox of PowerMock
  7. Disable static initializer in PowerMock

Overview

With PowerMock, you can make it return an arbitrary object when instantiated, or return an exception.

It is assumed that the constructor of the Utility class (mocking class) called from the UseUtility class (test target class) is mocked.

Tested class

This is the class to be tested that is called from JUnit.

I have instantiated the Utility class in the method, and I want to set the objects and exceptions returned at this time to arbitrary ones.

UseUtility.java


public class UseUtility {

	public void createUtility() throws Exception {

		Utility utility = new Utility();

		//Omitted thereafter
	}
}

Mocking class

Mock it and define arbitrary behavior in the constructor.

Utility.java


public class Utility {

	public Utility() throws Exception {
		//abridgement
	}
}

Test class

JUnit that calls the class under test.

To use PowerMock with JUnit, specify PowerMockRunner for @RunWith.

Note that in @PrepareForTest, not only the class to be mocked, but also the class that calls the constructor must be written.

UseUtilityTest.java


@RunWith(PowerMockRunner.class)
@PrepareForTest({ Utility.class, UseUtility.class })
public class UseUtilityTest {
    //abridgement
}

Example of use

thenReturn Set the instance returned by the constructor of the mocked class.

The mock is set to be returned here, but it does not have to be a mock.

@Test
public void test_thenReturn() throws Exception {
	//Preparation
	//Mocking
	Utility mock = PowerMockito.mock(Utility.class);
	//Set the instance to be returned in the constructor
	PowerMockito.whenNew(Utility.class).withNoArguments().thenReturn(mock);

	//Run
	UseUtility obj = new UseUtility();
	obj.createUtility();
}

thenThrow Set an exception that occurs in the constructor of the mocked class.

@Test(expected = Exception.class)
public void test_thenThrow() throws Exception {
	//Preparation
	Exception exception = new Exception("error!");

	//Mocking
	PowerMockito.mock(Utility.class);
	//Set exceptions that occur in the constructor
	PowerMockito.whenNew(Utility.class).withNoArguments().thenThrow(exception);

	//Run
	UseUtility obj = new UseUtility();
	obj.createUtility();
}

Constructor validation

PowerMock provides verifyNew for validating mocked constructors.

You can use Mockito's times, atLeast, asLeastOnce, etc. to verify the number of calls.

verifyNew (Utility.class) is synonymous with verifyNew (Utility.class, times (1)).

//Confirm that it was called once
PowerMockito.verifyNew(Utility.class).withNoArguments();

Recommended Posts

Mock the constructor with PowerMock
Mock Enums with PowerMock
Mock static methods with PowerMock
How to mock each case with PowerMock + Mockito1x
Arbitrate the fight between PowerMock and jacoco with Gradle
MOCK constructors of other classes with Spring MVC + PowerMock + Junit
Change the port with SpringBoot
What is the constructor for?
With the error Cordova-> Looper.prepare ()
Monitor the clipboard with JNA
I studied the constructor (java)
NoSuchMethodException with lombok without default constructor
Simulate the simplex method with GUI
Use constructor with arguments in cucumber-picocontainer
Automatically scroll the background with libGDX
Programming with ruby (on the way)
Follow the link with Selenium (Java)
Place in the middle with css
Mock only some methods with Spock
Follow the Datomic tutorial with Datascript
The guy who tries-with-resources with kotlin
Image processing: Let's play with the image