A software project is typically developed and tested as an independent unit. Using EasyMock in Maven allows you to simulate dependencies and verify behavior without relying on real implementations. It is especially helpful for testing methods (including void methods) by creating mock objects and defining expected interactions.
- Helps in mocking dependencies for unit testing
- Useful for testing void methods (no return value)
- Ensures correct behavior by verifying interactions
Dependency (pom.xml)
- Add EasyMock dependency with test scope
- Used only during testing phase
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>4.0.2</version>
<scope>test</scope>
</dependency>
void method
Void methods do not return any value, so they cannot be tested using output-based assertions. However, testing is still important because such methods may perform actions like saving data or updating states.
- Void methods don’t return values, so behavior is tested instead
- Use mocking tools (like EasyMock) to verify interactions
- Example: methods like save() may create data even without returning anything
Example Maven Project
Let us start with pom.xml, which contains all the dependencies required for the project.

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>easymock</artifactId>
<name>easymock</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.gfg</groupId>
<artifactId>testing-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>${easymock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<easymock.version>4.0.2</easymock.version>
</properties>
</project>
Mock a void method or a method that returns a valid datatype and it is to be available in an interface
public interface WeatherServiceInterface {
public BigDecimal getLocationTemperature(GeoLocation geoLocation) ;
}
If we mock a void method, return null needs to be provided for the success to be returned. Otherwise, according to the returned one, we can do it. The complete code for the rest of the java files is given below
GeoLocation.java
import java.math.BigDecimal;
public class GeoLocation {
private String locationName;
private BigDecimal minTemperature;
private BigDecimal maxTemperature;
@Override
public String toString() {
return "GeoLocation [locationName=" + locationName + ", minTemperature=" + minTemperature + ", maxTemperature="
+ maxTemperature + "]";
}
public GeoLocation(String locationName, BigDecimal minTemperature, BigDecimal maxTemperature) {
this.locationName = locationName;
this.minTemperature = minTemperature;
this.maxTemperature = maxTemperature;
}
public String getLocationName() {
return locationName;
}
public void setLocationName(String locationName) {
this.locationName = locationName;
}
public BigDecimal getMinTemperature() {
return minTemperature;
}
public void setMinTemperature(BigDecimal minTemperature) {
this.minTemperature = minTemperature;
}
public BigDecimal getMaxTemperature() {
return maxTemperature;
}
public void setMaxTemperature(BigDecimal maxTemperature) {
this.maxTemperature = maxTemperature;
}
public GeoLocation(String locationName) {
this.locationName = locationName;
}
}
WeatherServiceInterface.java
import java.math.BigDecimal;
public interface WeatherServiceInterface {
public BigDecimal getLocationTemperature(GeoLocation geoLocation);
}
WeatherForecastProcessor.java
import java.math.BigDecimal;
public class WeatherForecastProcessor {
private WeatherServiceInterface weatherService;
public BigDecimal getMaximumTemperature(GeoLocation location) {
BigDecimal temperature = null;
temperature = weatherService.getLocationTemperature(location);
return temperature;
}
public WeatherServiceInterface getWeatherService() {
return weatherService;
}
public void setWeatherService(WeatherServiceInterface weatherService) {
this.weatherService = weatherService;
}
}
WeatherServiceUnavailableException.java
public class WeatherServiceUnavailableException extends Exception {
private static final long serialVersionUID = 6961151537340723535L;
}
Test File
WeatherForecastProcessorUnitTest.java
package com.gfg.easymocktesting;
import java.math.BigDecimal;
import org.easymock.EasyMock;
import org.junit.jupiter.api.Test;
import junit.framework.TestCase;
public class WeatherForecastProcessorUnitTest extends TestCase {
private com.gfg.easymocktesting.WeatherForecastProcessor weatherForecastProcessor;
private com.gfg.easymocktesting.WeatherServiceInterface mockWeatherServiceInterface;
com.gfg.easymocktesting.GeoLocation location;
BigDecimal temperature;
@Test
public void testGetMaximumTemperature() {
weatherForecastProcessor = new WeatherForecastProcessor();
mockWeatherServiceInterface = EasyMock.createMock(WeatherServiceInterface.class);
location = new GeoLocation("London", new BigDecimal(10.00), new BigDecimal(100.00));
weatherForecastProcessor.setWeatherService(mockWeatherServiceInterface);
EasyMock.expect(mockWeatherServiceInterface.getLocationTemperature(location)).andReturn(temperature);
EasyMock.replay(mockWeatherServiceInterface);
}
}
JUnit Testcase execution output

Actually for doing a mocking a database connection or the properties retrieved from a file available locally or from the server are not needed. Easymock has used the Java reflection concept and with this, they have used mock objects for a given interface.