Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Test a Maven Project using EasyMock?

Improve Article
Save Article
  • Last Updated : 23 Nov, 2022
Improve Article
Save Article

Always a software project is prepared and tested as an individual unit. We pass different scenarios and expect the original values should match. One such approach is “Easymock” and in this article via a sample project, handled when and how to mock void methods. We are going to see this approach via the maven project. Hence dependencies need to be added in pom.xml

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

void method:

Usually, methods tend to return a value and testing can be done by passing a value and expecting the method is returning that value or not. If the methods are not returning anything, then they are called void methods and in those cases, whether testing is required or not is the major factor. But essentially it is required as though the method does not return anything, in the case of Session.save(), it creates a new ID in the system. During such scenarios, we can mock and test the dependencies. Before going to the programs, let us see the project structure.

 

Let us start with pom.xml, which contains all the dependencies required for the project.

Example Maven Project

pom.xml

XML




<?xml version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                        http://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

Java




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

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

Java




import java.math.BigDecimal;
  
public interface WeatherServiceInterface {
    public BigDecimal  getLocationTemperature(GeoLocation geoLocation);  
}


WeatherForecastProcessor.java

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

Java




public class WeatherServiceUnavailableException extends Exception {
    private static final long serialVersionUID = 6961151537340723535L;
}


Test File

WeatherForecastProcessorUnitTest.java

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

 

Conclusion

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.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!