EasyMock TestNG Example

Filed Under: EasyMock

In the EasyMock Tutorial, we used it with JUnit 5. In this tutorial, we will learn how to integrate EasyMock with TestNG testing framework.

EasyMock TestNG Example

We will build up our EasyMock TestNG example from the earlier tutorial. First of all, we will have to add the TestNG dependency to the already existing EasyMock example project.


Here is a simple example where I am mocking ArrayList and stubbing its behaviors. Then I am using TestNG assertions to write some test cases.

package com.journaldev.easymock;

import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.mock;
import static org.easymock.EasyMock.replay;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

import java.util.ArrayList;

import org.testng.annotations.Test;

public class EasyMockMethodTestNGExample {

	public void test() {
		ArrayList<Integer> mockList = mock(ArrayList.class);


		assertTrue(mockList.get(0) == 10);
		assertEquals(mockList.size(), 2);

EasyMock TestNG Annotations Example

Let’s look at another example where I will use EasyMock annotations with TestNG annotations.

package com.journaldev.easymock;

import static org.easymock.EasyMock.*;
import static org.testng.Assert.assertEquals;

import org.easymock.EasyMockSupport;
import org.easymock.Mock;
import org.easymock.TestSubject;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.journaldev.utils.Calculator;
import com.journaldev.utils.MathUtils;

public class EasyMockAnnotationTestNGExample {

	private Calculator mockCalculator;

	private MathUtils mathUtils = new MathUtils(mockCalculator);

	public void setup() {

	public void test() {
		expect(mockCalculator.add(1, 1)).andReturn(2);
		expect(mockCalculator.multiply(10, 10)).andReturn(100);

		assertEquals(mathUtils.add(1, 1), 2);
		assertEquals(mathUtils.multiply(10, 10), 100);


EasyMock integrates very easily with the TestNG framework just like JUnit. Actually, it’s very easy to switch between TestNG and JUnit while working with the EasyMock mocking framework. All we need is to change few import statements for assertions and testing lifecycle and callback methods.

You can checkout complete project and more EasyMock examples from our GitHub Repository.
Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors