EasyMock
Developer(s) | Tammo Freese |
---|---|
Stable release |
3.4[1]
/ 05 September 2015 |
Repository |
github |
Written in | Java |
Operating system | Cross-platform |
Type | Unit testing tool |
License | Apache License |
Website |
easymock |
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 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.[6][7]
Usage
EasyMock can be an ideal solution for 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;
}
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;
}
}
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);
}
}
See also
- Behavior driven development
- Mock object
- List of unit testing frameworks
- Software testing
- Unit testing
References
- ↑ EasyMock Releases
- ↑ "EasyMock License". EasyMock. EasyMock. Retrieved 11 January 2015.
- 1 2 3 4 Harold, E.R. (28 April 2008). "Easier testing with EasyMock". IBM. International Business Machines Corporation. Retrieved 11 January 2015.
- ↑ Weiss, Tal (26 November 2013). "GitHub's 10,000 most Popular Java Projects – Here are The Top Libraries They Use". Retrieved 11 January 2015.
- 1 2 Freese, T., EasyMock: Dynamic Mock Objects for JUnit, Oldenburg, Germany: Institute for Computer Science
- ↑ "Contributors". EasyMock. EasyMock. Retrieved 11 January 2015.
- ↑ Lüppken, S.; Stũble, M.; Stauble, M. (2009). Spring Web Flow 2 Web Development. Olton, UK: Packt Publishing. p. 191.