Jump to content

User:Shevonsilva/sandbox/Aspose API

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Shevonsilva (talk | contribs) at 05:39, 11 January 2016. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
EasyMock
Developer(s)Tammo Freese
Stable release
3.4[1] / Error: All values must be integers (help)
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]


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


Distinguishing 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 can 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.[6]Stauble, M. (2009). Spring Web Flow 2 Web Development. Olton, UK: Packt Publishing. p. 191.</ref>

Usage

EasyMock can be an ideal solution for application with often-changing interfaces.[5]

Example

Simple currency exchange program is provided here. An interface my 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 looks 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. ^ [1]
  2. ^ "EasyMock License". EasyMock. EasyMock. Retrieved 11 January 2015.
  3. ^ a b c d Harold, E.R. (28 April 2008). "Easier testing with EasyMock". IBM. International Business Machines Corporation. Retrieved 11 January 2015.
  4. ^ Weiss, Tal (26 November 2013). "GitHub's 10,000 most Popular Java Projects – Here are The Top Libraries They Use". Retrieved January 11 2015. {{cite web}}: Check date values in: |accessdate= (help)
  5. ^ a b Freese, T., EasyMock: Dynamic Mock Objects for JUnit, Oldenburg, Germany: Institute for Computer Science
  6. ^ "Contributors". EasyMock. EasyMock. Retrieved 11 January 2015.