[JAVA] Try using PowerMock's WhiteBox

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
  5. Mock private methods with PowerMock
  6. Try using PowerMock's WhiteBox ← Now here
  7. Disable static initializer in PowerMock


In Java, you can use a feature called reflection to manipulate visibility to get values for private fields and access private methods.

PowerMock provides these features in the WhiteBox class. (Reflection is used internally.)

Target class


public class SampleEm {

	private String field = null;

	public SampleEm(String filed) {

	public String getField() {
		return field;

	private void setField(String filed) {
		this.field = filed;

Example of use

Get the value of a private field

getInternalState (instance, field name string)

public void test_getInternalState() throws Exception {
	SampleEm em = new SampleEm("value");
	//Get the value of a private field
	String field = Whitebox.getInternalState(em, "field");

Set a value in the private field

setInternalState (instance, field name string, set value)

public void test_setInternalState() throws Exception {
	SampleEm em = new SampleEm("value");
	//Set a value in the private field
	Whitebox.setInternalState(em, "field", "newValue");

Call a private method

ʻInvokeMethod (instance, method name string, argument, ...) `

public void test_invokeMethod() throws Exception {
	SampleEm em = new SampleEm("value");
	//Call a private method
	Whitebox.invokeMethod(em, "setField", "newValue");

Various other methods are provided.

You can access private fields / methods using reflection with your own code, but I think the advantage is that you can keep the test code simple by using Whitebox.

Recommended Posts

Try using PowerMock's WhiteBox
Try using libGDX
Try using Maven
Try using powermock-mockito2-2.0.2
Try using GraalVM
Try using jmockit 1.48
Try using sql-migrate
Try using SwiftLint
Try using Log4j 2.0
Try using Axon Framework
Try using JobScheduler's REST-API
Try using java.lang.Math methods
Try using Talend Part 2
Try using Talend Part 1
Try using F # list
Try using each_with_index method
Try using Spring JDBC
Try using RocksDB in Java
Try using GloVe with Deeplearning4j
Try using view_component with rails
Try scraping using java [Notes]
Try using Cocoa from Ruby
Try using letter_opener_web for inquiries
[Swift] Try using Collection View
Try using IntelliJ IDEA once
Try using Spring Boot Security
Try using gRPC in Ruby
[Rails] Try using Faraday middleware
[Processing] Try using GT Force.
[Programming Encyclopedia] §2 Try using Ruby
People using docker Try using docker-compose
Try using Redmine on Mac docker
Try using Redis with Java (jar)
[Java] Try to implement using generics
Try using the messaging system Pulsar
Try using IBM Java method tracing
Try using Hyperledger Iroha's Java SDK
[Java] Where did you try using java?
Try using Java framework Nablarch [Web application]
Try using || instead of the ternary operator
Try using the Stream API in Java
Try using the Rails API (zip code)
Study Java Try using Scanner or Map
Try using JSON format API in Java
Try using Spring Boot with VS Code
Try using Reladomo's MT Loader (Multi-Threaded Matcher Loader)
Try using JobScheduler's REST-API --Java RestClient implementation--
Try using Kong + Konga with Docker Compose.
Try using the Emotion API from Android
Try using the Wii remote with Java
Try using simple_form / edit even child models
Try implementing GraphQL server using grahpql-java-tools (+ kotlin)