Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

EasyMock lets you replace a class’s collaborators with configurable mock objects, so a JUnit test can check how the class under test interacts with them. The essential sequence is to create a mock, record expected calls, replay it, run the code under test, and verify the expectations.

Add EasyMock to a Maven project

Add EasyMock as a test dependency in your project’s pom.xml. The official guide currently lists version 5.7.0; check the EasyMock user guide for the version available when you set up your project.

<dependency>
  <groupId>org.easymock</groupId>
  <artifactId>easymock</artifactId>
  <version>5.7.0</version>
  <scope>test</scope>
</dependency>

The guide also describes a standalone ZIP containing easymock-5.7.0.jar. Class mocking may additionally require Objenesis.

Understand the record/replay/verify lifecycle

EasyMock tests focus on interactions: which calls the class under test makes to its collaborators, and with what arguments. The project describes the purpose succinctly: “Mock Objects replace collaborators of the unit under test.” — EasyMock project documentation.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Create: Make a mock for the collaborator.
  2. Record: Call the mock with the arguments you expect the class under test to use.
  3. Replay: Switch the mock from recording mode to testing mode with replay(mock).
  4. Execute: Call the method being tested.
  5. Verify: Call verify(mock) to check that the recorded expectations occurred.

For example, this JUnit 4 test expects the class to notify its listener when it adds a document:

import static org.easymock.EasyMock.*;
import org.junit.Before;
import org.junit.Test;

public class ClassTestedTest {
  private ClassTested classUnderTest;
  private Collaborator collaborator;

  @Before
  public void setUp() {
    collaborator = mock(Collaborator.class);
    classUnderTest = new ClassTested();
    classUnderTest.setListener(collaborator);
  }

  @Test
  public void addDocument_notifiesCollaborator() {
    collaborator.documentAdded("New Document");
    replay(collaborator);

    classUnderTest.addDocument("New Document", "content");

    verify(collaborator);
  }
}

The call to documentAdded happens before replay, so it records the expected interaction rather than executing it. After replay, an unexpected call or an argument that does not match the expectation fails the test; verification detects expected calls that never happened. As the EasyMock getting-started guide puts it, “Any other call to our mock is a test failure.”

Choose the right JUnit integration

JUnit 4: runner, rule, or manual setup

For JUnit 4, EasyMock supports field annotations such as @Mock and @TestSubject. Add @RunWith(EasyMockRunner.class) to let the runner process those fields; this runner requires JUnit 4.5 or later. If your test already needs a different JUnit 4 runner, use EasyMockRule instead. Manual mock creation and setup, as in the earlier example, is another option. See the EasyMock user guide for the annotation and rule details.

JUnit 5: register EasyMockExtension

JUnit 5 uses extensions rather than JUnit 4’s single-runner model. Register EasyMockExtension with @ExtendWith, then declare annotated fields:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import org.easymock.Mock;
import org.easymock.TestSubject;
import org.easymock.EasyMockExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.easymock.EasyMock.*;

@ExtendWith(EasyMockExtension.class)
class ClassTestedTest {
  @Mock Collaborator collaborator;
  @TestSubject ClassTested classUnderTest = new ClassTested();

  @Test
  void addDocument_notifiesCollaborator() {
    collaborator.documentAdded("New Document");
    replay(collaborator);
    classUnderTest.addDocument("New Document", "content");
    verify(collaborator);
  }
}

EasyMock’s guide says JUnit 5 extension support has been available since EasyMock 4.1. The extension is documented as a JUnit TestInstancePostProcessor; consult the EasyMockExtension API reference for its API details.

Decide whether call order matters

Choose a mock type based on whether order and unspecified calls are part of the behavior you want to test:

Rank #4
Sale
EasyMock option Call order Unspecified calls Best fit
mock() Not checked Normal EasyMock expectation behavior; unexpected calls fail Order is incidental
strictMock() Checked Unexpected calls fail The interaction sequence is part of the contract
niceMock() Not checked Returns default values Extra calls are harmless and defaults are meaningful
partialMockBuilder() Only configured methods are mocked Other methods retain real implementation behavior A narrow seam is needed in a class with real behavior to preserve

Do not make a test strict just to specify more interactions: strict mocks are appropriate when a sequence is observable and matters to correctness. With a partial mock, EasyMock does not let you hide private methods; test private behavior through the class’s public behavior instead. These distinctions are described in the EasyMock user guide.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Manage several mocks with EasyMockSupport

When a test uses several collaborators, EasyMockSupport can centralize mock management and let you call replayAll() and verifyAll() rather than naming every mock at each stage. This reduces repetitive lists in larger tests. For a small test with one or two collaborators, explicit replay and verify calls can make the lifecycle easier to follow. The guide documents EasyMockSupport and its lifecycle methods.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value

Know what an EasyMock test establishes

An EasyMock test can check that the class under test makes the expected calls to a collaborator. It does not establish that the collaborator’s real implementation works, because the mock replaces that implementation. Keep interaction expectations focused on behavior that matters: overspecifying calls can make a test fail when internal interactions change without changing the externally relevant result.

Quick Recap

SaleBestseller No. 3
SaleBestseller No. 4
Pragmatic Unit Testing in Java with JUnit
Pragmatic Unit Testing in Java with JUnit
Used Book in Good Condition
$13.88
SaleBestseller No. 5

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.