EasyMock
| Developer(s) | Tammo Freese Henri Tremblay |
|---|---|
| Stable release | 5.0.1
/ October 24, 2022[1] |
| Written in | Java |
| Engine | |
| Operating system | Cross-platform |
| Type | Unit testing tool |
| License | Apache License |
| Website | easymock |
Search EasyMock on Amazon.
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 a Mock Object differs 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 applications with often-changing interfaces.[5]
Example
A simple currency exchange program is provided here. An interface may look as follows:
import java.io.IOException;
public interface ExchangeRate {
double getRate(String inputCurrency, String outputCurrency) throws IOException;
}
Implementation for a concrete class may look 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 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. Retrieved 11 January 2015.
- ↑ 3.0 3.1 3.2 3.3 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.
- ↑ 5.0 5.1 Freese, T., EasyMock: Dynamic Mock Objects for JUnit, Oldenburg, Germany: Institute for Computer Science
- ↑ "Contributors". 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. Search this book on
External links
This article "EasyMock" is from Wikipedia. The list of its authors can be seen in its historical and/or the page Edithistory:EasyMock. Articles copied from Draft Namespace on Wikipedia could be seen on the Draft Namespace of Wikipedia and not main one.
