EasyMock Partial Mock

Filed Under: EasyMock

EasyMock supports creating partial mock, where we can specify the methods that will be mocked. If any other method is called, object real method will get invoked.

EasyMock Partial Mock Example

Let’s say we have a utility class defined as:

package com.journaldev.utils;

public class StringUtils {

	public void print(String s) {

	public String toUpperCase(String s) {
		return s.toUpperCase();

We want to mock toUpperCase() method but not print() method. We can use partialMockBuilder() method to achieve this. We will also have to use addMockedMethod() to provide the list of methods to be mocked.

package com.journaldev.easymock;

import static org.easymock.EasyMock.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import com.journaldev.utils.StringUtils;

public class EasyMockPartialMockingExample {

  public void test() {
    StringUtils mock = partialMockBuilder(StringUtils.class)
    //real method will be called, check the console
    //testing mocked method
    assertEquals("JAVA", mock.toUpperCase("java"));

If you want to mock multiple methods in the partial mock, use addMockedMethods() with the method names as argument.

EasyMock Partial Mock Constructor

We can call a class constructor too while creating the partial mock. This is useful when the real methods are using some properties set through the constructor.

For calling a partial mock constructor, we can use withConstructor() and withArgs() method. There are multiple overloaded withConstructor() methods, chose one that fits your class constructor requirements.

Let’s say we have a Data class defined as:

class Data {
  private String message;
  private String encodedMsg;
  public Data(String msg) {
    this.message = msg;
  public void print() {
    System.out.println("Message is: "+this.message);
  public String encode() {
    this.encodedMsg = Base64.getEncoder().encodeToString(this.message.getBytes());
    return encodedMsg;
  public String decode() {
    return String.valueOf(Base64.getDecoder().decode(encodedMsg));

We want to mock only decode() method. But since print() and encode() methods are using object property being set through the constructor, we will have to create partial mock and call its constructor too.

package com.journaldev.easymock;

import static org.easymock.EasyMock.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Base64;

import org.junit.jupiter.api.Test;

public class EasyMockPartialMockConstructorExample {

  public void test() {
    Data mock = partialMockBuilder(Data.class)
    //calling real methods
    String encodedMessage = mock.encode();
    assertEquals(Base64.getEncoder().encodeToString("Hello".getBytes()), mock.encode());
    //mock some behaviors
    assertEquals("Hello", mock.decode());


EasyMock partial mock is helpful when we are interested in testing only a few methods of the class. It’s very similar to creating partial mock in Mockito using Mockito Spy.

You can checkout complete project and more EasyMock examples from our GitHub Repository.

Leave a Reply

Your email address will not be published. Required fields are marked *

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages