EasyMock

Last updated
EasyMock
Developer(s) Tammo Freese Henri Tremblay
Stable release
5.0.1 / October 24, 2022;2 months ago (2022-10-24) [1]
Repository
Written in Java
Operating system Cross-platform
Type Unit testing tool
License Apache License
Website easymock.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]

Contents

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:

importjava.io.IOException;publicinterfaceExchangeRate{doublegetRate(StringinputCurrency,StringoutputCurrency)throwsIOException;}

[3]

Implementation for a concrete class may look like as follows:

importjava.io.IOException;publicclassCurrency{privateStringunits;privatelongamount;privateintcents;publicCurrency(doubleamount,Stringcode){this.units=code;setAmount(amount);}privatevoidsetAmount(doubleamount){this.amount=newDouble(amount).longValue();this.cents=(int)((amount*100.0)%100);}publicCurrencytoEuros(ExchangeRateconverter){if("EUR".equals(units))returnthis;else{doubleinput=amount+cents/100.0;doublerate;try{rate=converter.getRate(units,"EUR");doubleoutput=input*rate;returnnewCurrency(output,"EUR");}catch(IOExceptionex){returnnull;}}}publicbooleanequals(Objecto){if(oinstanceofCurrency){Currencyother=(Currency)o;returnthis.units.equals(other.units)&&this.amount==other.amount&&this.cents==other.cents;}returnfalse;}publicStringtoString(){returnamount+"."+Math.abs(cents)+" "+units;}}

[3]

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

importjunit.framework.TestCase;importorg.easymock.EasyMock;importjava.io.IOException;publicclassCurrencyTestextendsTestCase{publicvoidtestToEuros()throwsIOException{CurrencytestObject=newCurrency(2.50,"USD");Currencyexpected=newCurrency(3.75,"EUR");ExchangeRatemock=EasyMock.createMock(ExchangeRate.class);EasyMock.expect(mock.getRate("USD","EUR")).andReturn(1.5);EasyMock.replay(mock);Currencyactual=testObject.toEuros(mock);assertEquals(expected,actual);}}

[3]

See also

Related Research Articles

JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks which is collectively known as xUnit that originated with SUnit.

In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be divided between classes with unique areas of concern as well as to the Open-Closed Principle, by allowing the functionality of a class to be extended without being modified. Decorator use can be more efficient than subclassing, because an object's behavior can be augmented without defining an entirely new object.

In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled.

In computer programming, unit testing is a software testing method by which individual units of source code—sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures—are tested to determine whether they are fit for use.

<span class="mw-page-title-main">NUnit</span>

NUnit is an open-source unit testing framework for the .NET Framework and Mono. It serves the same purpose as JUnit does in the Java world, and is one of many programs in the xUnit family.

<span class="mw-page-title-main">Java syntax</span> Set of rules defining correctly structured programs

The syntax of Java refers to the set of rules defining how a Java program is written and interpreted.

In computing based on the Java Platform, JavaBeans is a technology developed by Sun Microsystems and released in 1996, as part of JDK 1.1.

<span class="mw-page-title-main">Dependency injection</span> Software programming technique

In software engineering, dependency injection is a design pattern in which an object or function receives other objects or functions that it depends on. A form of inversion of control, dependency injection aims to separate the concerns of constructing objects and using them, leading to loosely coupled programs. The pattern ensures that an object or function which wants to use a given service should not have to know how to construct those services. Instead, the receiving 'client' is provided with its dependencies by external code, which it is not aware of. Dependency injection helps by making implicit dependencies explicit and helps solve the following problems:

TestNG is a testing framework for the Java programming language created by Cédric Beust and inspired by JUnit and NUnit. The design goal of TestNG is to cover a wider range of test categories: unit, functional, end-to-end, integration, etc., with more powerful and easy-to-use functionalities.

In object-oriented programming, mock objects are simulated objects that mimic the behaviour of real objects in controlled ways, most often as part of a software testing initiative. A programmer typically creates a mock object to test the behaviour of some other object, in much the same way that a car designer uses a crash test dummy to simulate the dynamic behaviour of a human in vehicle impacts. The technique is also applicable in generic programming.

<span class="mw-page-title-main">Oxygene (programming language)</span> Object Pascal-based programming language

Oxygene is a programming language developed by RemObjects Software for Microsoft's Common Language Infrastructure, the Java Platform and Cocoa. Oxygene is based on Delphi's Object Pascal, but also has influences from C#, Eiffel, Java, F# and other languages.

<span class="mw-page-title-main">YUI Library</span>

The Yahoo! User Interface Library (YUI) is a discontinued open-source JavaScript library for building richly interactive web applications using techniques such as Ajax, DHTML, and DOM scripting. YUI includes several core CSS resources. It is available under a BSD License. Development on YUI began in 2005 and Yahoo! properties such as My Yahoo! and the Yahoo! front page began using YUI in the summer of that year. YUI was released for public use in February 2006. It was actively developed by a core team of Yahoo! engineers.

Stripes is an open source web application framework based on the model–view–controller (MVC) pattern. It aims to be a lighter weight framework than Struts by using Java technologies such as annotations and generics that were introduced in Java 1.5, to achieve "convention over configuration". This emphasizes the idea that a set of simple conventions used throughout the framework reduce configuration overhead. In practice, this means that Stripe applications barely need any configuration files, thus reducing development and maintenance work. It has been dormant since 2016.

In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL). The term was coined in 2005 by Eric Evans and Martin Fowler.

Hamcrest is a framework that assists writing software tests in the Java programming language. It supports creating customized assertion matchers, allowing match rules to be defined declaratively. These matchers have uses in unit testing frameworks such as JUnit and jMock. Hamcrest has been included in JUnit 4 since 2012, but was omitted from JUnit 5 in 2017.

Mockito is an open source testing framework for Java released under the MIT License. The framework allows the creation of test double objects in automated unit tests for the purpose of test-driven development (TDD) or behavior-driven development (BDD).

<span class="mw-page-title-main">Jasmine (software)</span> Open-source testing framework for JavaScript

Jasmine is an open-source testing framework for JavaScript. It aims to run on any JavaScript-enabled platform, to not intrude on the application nor the IDE, and to have easy-to-read syntax. It is heavily influenced by other unit testing frameworks, such as ScrewUnit, JSSpec, JSpec, and RSpec.

Concordion is a specification by example framework originally developed by David Peterson, and now maintained by a team of contributors, led by Nigel Charman.

Cuppa is a behavior-driven development (BDD) unit testing framework for the Java programming language version 8. The framework uses features introduced in version 8 of the language, such as lambdas. It is inspired by Mocha.

References

  1. EasyMock Releases
  2. "EasyMock License". EasyMock. EasyMock. Retrieved 11 January 2015.
  3. 1 2 3 4 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 11 January 2015.
  5. 1 2 Freese, T., EasyMock: Dynamic Mock Objects for JUnit, Oldenburg, Germany: Institute for Computer Science
  6. "Contributors". EasyMock. EasyMock. Retrieved 11 January 2015.
  7. Lüppken, S.; Stũble, M.; Stauble, M. (2009). Spring Web Flow 2 Web Development. Olton, UK: Packt Publishing. p. 191.