Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Mocking With EasyMock

DZone's Guide to

Mocking With EasyMock

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

· Java Zone ·
Free Resource

The CMS developers love. Open Source, API-first and Enterprise-grade. Try BloomReach CMS 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.

BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.

Topics:
mocking ,easymock ,java

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}