Software:EasyMock

From HandWiki
EasyMock
Developer(s)Tammo Freese Henri Tremblay
Stable release
5.0.1 / October 24, 2022; 20 months ago (2022-10-24)[1]
Written inJava
Operating systemCross-platform
TypeUnit testing tool
LicenseApache License
Websiteeasymock.org

EasyMock is an open-source testing framework for Java released under the Apache License.[2] The framework allows the creation of test double objects for the purpose of Test-driven Development (TDD) or Behavior Driven Development (BDD).[3]

Research performed in 2013 on 10,000 GitHub projects found that EasyMock is the 32nd most popular Java library.[4]

Features

The EasyMock provides dynamically generated Mock objects (at runtime), without having to implement them. In EasyMock, the definition of Mock Object is differed from using an implemented Mock Object. Mock objects are built at run time and additional implementations cannot be defined for those objects.[5]

Origin

EasyMock was created by Tammo Freese in 2001 (at OFFIS). Originally it allowed only mock interfaces, with type safe mocking and additional features were added in later developments. Most notably, class mocking was added by Henri Tremblay, the current lead developer, in 2003.[6][7]

Usage

EasyMock can be used in application with often-changing interfaces.[5]

Example

Simple currency exchange program is provided here. An interface may look like as follows:

import java.io.IOException;

public interface ExchangeRate {

    double getRate(String inputCurrency, String outputCurrency) throws IOException;

}

[3]

Implementation for a concrete class may look like as follows:

import java.io.IOException;

public class Currency {

    private String units;
    private long amount;
    private int cents;

    public Currency(double amount, String code) {
        this.units = code;
        setAmount(amount);
    }

    private void setAmount(double amount) {
        this.amount = new Double(amount).longValue();
        this.cents = (int) ((amount * 100.0) % 100);
    }

    public Currency toEuros(ExchangeRate converter) {
        if ("EUR".equals(units)) return this;
        else {
            double input = amount + cents/100.0;
            double rate;
            try {
                rate = converter.getRate(units, "EUR");
                double output = input * rate;
                return new Currency(output, "EUR");
            } catch (IOException ex) {
                return null;
            }
        }
    }

    public boolean equals(Object o) {
        if (o instanceof Currency) {
            Currency other = (Currency) o;
            return this.units.equals(other.units)
                    && this.amount == other.amount
                    && this.cents == other.cents;
        }
        return false;
    }

    public String toString() {
        return amount + "." + Math.abs(cents) + " " + units;
    }

}

[3]

Sample implementation for a test class may look like as follows:

import junit.framework.TestCase;
import org.easymock.EasyMock;
import java.io.IOException;

public class CurrencyTest extends TestCase {

    public void testToEuros() throws IOException {
        Currency testObject = new Currency(2.50, "USD");
        Currency expected = new Currency(3.75, "EUR");
        ExchangeRate mock = EasyMock.createMock(ExchangeRate.class);
        EasyMock.expect(mock.getRate("USD", "EUR")).andReturn(1.5);
        EasyMock.replay(mock);
        Currency actual = testObject.toEuros(mock);
        assertEquals(expected, actual);
    }

}

[3]

See also

References

  1. EasyMock Releases
  2. "EasyMock License". EasyMock. http://easymock.org/license.html. 
  3. 3.0 3.1 3.2 3.3 Harold, E.R. (28 April 2008). "Easier testing with EasyMock". International Business Machines Corporation. http://www.ibm.com/developerworks/library/j-easymock/. 
  4. Weiss, Tal (26 November 2013). "GitHub’s 10,000 most Popular Java Projects – Here are The Top Libraries They Use". http://www.takipiblog.com/2013/11/26/githubs-10000-most-popular-java-projects-here-are-the-top-libraries-they-use/. 
  5. 5.0 5.1 Freese, T., EasyMock: Dynamic Mock Objects for JUnit, Oldenburg, Germany: Institute for Computer Science 
  6. "Contributors". EasyMock. http://easymock.org/contributors.html. 
  7. Lüppken, S.; Stũble, M.; Stauble, M. (2009). Spring Web Flow 2 Web Development. Olton, UK: Packt Publishing. pp. 191. 

External links