DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Mocking With EasyMock

Mocking With EasyMock

Here is a real-world example of use mocking to test the functionality as it provides some mocked data.

Arun Pandey user avatar by
Arun Pandey
·
Nov. 08, 16 · Opinion
Like (8)
Save
Tweet
Share
13.21K Views

Join the DZone community and get the full member experience.

Join For Free

What Is Mocking?

Mocking is an important part of unit testing. In some cases where data/resource may not be available (probably not implemented) or available over network, accessing those data/resource during unit-testing may cause a blocker/slower the development. So we can use mocking to test the functionality as it provides some mocked data.

I am considering a real world example here to get a better understanding of mocking and its importance.

Let us consider an FX (Foreign Exchange) software which we are working on and where FX rates are coming from different sources, so we can not test our FX software with real data and we need mocking to test this.

Let us see the below example for FX software.

Required Jars : easymock-3.0.jar, asm.jar, cglib-2.1.3.jar

FxExchangeService.java

package test.easymock;

public interface FxExchangeService {
  public double getFxExchangeRate(String ccy1, String ccy2);
}

FxOrder.java

package test.easymock;

public class FxOrder {

  private String ccy1;
  private String ccy2;
  private int quantity;

  public FxOrder(String ccy1, String ccy2, int quantity){
    this.ccy1 = ccy1;
    this.ccy2 = ccy2;
    this.quantity = quantity;
  }

  public String getCcy1() {
    return ccy1;
  }
  public void setCcy1(String ccy1) {
    this.ccy1 = ccy1;
  }
  public String getCcy2() {
    return ccy2;
  }
  public void setCcy2(String ccy2) {
    this.ccy2 = ccy2;
  }
  public int getQuantity() {
    return quantity;
  }
  public void setQuantity(int quantity) {
    this.quantity = quantity;
  }
}

FxPortfolio.java

package test.easymock;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FxPortfolio {

  private String userName;
  private double totalValue;
  private Date date;

  FxExchangeService fxExchangeService;
  List<FxOrder> fxOrderList = new ArrayList<FxOrder>();

  public String getUserName() {
    return userName;
  }
  public void setUserName(String userName) {
    this.userName = userName;
  }
  public void setTotalValue(double totalValue) {
    this.totalValue = totalValue;
  }
  public Date getDate() {
    return date;
  }
  public void setDate(Date date) {
    this.date = date;
  }

  public Double getTotalFxValue() {

    for(FxOrder fxOrder : this.fxOrderList) {
      totalValue += ((fxExchangeService.getFxExchangeRate(fxOrder.getCcy1(), fxOrder.getCcy2())) * fxOrder.getQuantity());
    }
    return totalValue;
  }

  public List<FxOrder> getFxOrderList() {
    return fxOrderList;
  }
  public void addFxOrder(FxOrder fxOrder) {
    this.fxOrderList.add(fxOrder);
  }
  public FxExchangeService getFxExchangeService() {
    return fxExchangeService;
  }
  public void setFxExchangeService(FxExchangeService fxExchangeService) {
    this.fxExchangeService = fxExchangeService;
  }

}

Now let us see the testing performed using EasyMock as below:

EasyMockTest.java

package test.easymock;

import junit.framework.Assert;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;

public class EasyMockTest {

  private FxExchangeService marketMock = null;
  private FxPortfolio fxPortfolio;

  @Before
  public void setUp() {
    fxPortfolio = new FxPortfolio();
    marketMock = EasyMock.createMock(FxExchangeService.class);
    fxPortfolio.setFxExchangeService(marketMock);
  }

  @Test
  public void testGetTotalFxValue() {
    EasyMock.expect(marketMock.getFxExchangeRate("USD", "INR")).andReturn(52.00);
    EasyMock.replay(marketMock);                //replay() method, to make the mock object ready to use.

    FxOrder fxOrder = new FxOrder("USD", "INR", 10);
    fxPortfolio.addFxOrder(fxOrder);   
    Assert.assertEquals(52*10.0, fxPortfolio.getTotalFxValue());
  }
}

 

So mocking helps to improve unit-tests by removing the external dependencies to get a better, faster and independent unit tests.

unit test EasyMock

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Fixing Bottlenecks in Your Microservices App Flows
  • Microservices 101: Transactional Outbox and Inbox
  • Seamless Integration of Azure Functions With SQL Server: A Developer's Perspective
  • How To Select Multiple Checkboxes in Selenium WebDriver Using Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: