EasyMock
EasyMock
|
|
---|---|
![]() |
|
Basic data
|
|
Maintainer | Tammo Freese, Henri Tremblay |
developer | OFFIS , Tammo Freese, Henri Tremblay |
Current version |
4.2 ( January 29, 2020 ) |
operating system | Cross-platform |
programming language | Java |
category | Test - Framework |
License | Apache license 2.0 |
easymock.org |
EasyMock is a program library for creating mock objects for unit tests of Java programs.
In the unit test , individual test objects (mostly classes or methods ) should be tested in isolation from their environment. In order to achieve a completely isolated test, the interfaces through which the object to be tested accesses its environment must be replaced by mock objects . The mock objects act as placeholders for the real objects.
Functionality
EasyMock is the first program library that supports the dynamic creation of mock objects based on their interface. It was first presented at the OOPSLA in 2001. The behavior of a dynamic mock object is not programmed in its own class, but recorded before the unit test. The dynamic creation of mock objects approach offers several advantages over static programming of mock classes and objects.
- There are no classes to be written by hand.
- There is no need to keep the source code of the mock classes synchronized with that of the real classes .
- Dynamic mock objects are safer than refactoring
The following steps are carried out to use EasyMock:
- Generate a mock object of the class or interface to be simulated and transfer it to the object to be tested.
- Record expected behavior (based on the fluent interface - design patterns ).
- Set the mock object to replay mode .
- Verifying ( verify (mock) ) whether the mock object was also used as specified in step two.
Typical example
The behavior of the customer service class is to be verified by a unit test . This class has a reference to an auxiliary class KundeDAO , which reads the customer data from a database . The CustomerDAO data access object must be replaced by a mock object in order to be able to test the KundeService class in isolation.
For the first step, the setUp () method of the JUnit test is overwritten:
private IKundeDAO kundeDAOMock;
private KundeService kundeService;
@Before
protected void setUp() throws Exception {
super.setUp();
//Schritt 1: Mock-Objekt erstellen
kundeDAOMock = EasyMock.createMock(IKundeDAO.class);
// KundenService erzeugen
kundeService = new KundeService();
// KundenService mit Mock-Objekt versorgen
kundeService.setKundeDAO(kundeDAOMock);
}
Steps 2 and 3 are carried out in the test method:
@Test
public void testKundenLesen() {
//Schritt 2: Erwartetes Verhalten vom KundeDAO aufzeichnen
// erwartetes Ergebnis erstellen
List<Kunde> list = new ArrayList<Kunde>();
list.add(new Kunde("Müller"));
list.add(new Kunde("Meier"));
// erwarteten Methodenaufruf am Mock-Objekt mit Ergebnis aufzeichnen
EasyMock.expect(kundeDAOMock.findAll()).andReturn(list);
//Schritt 3: Aufnahme beendet!
EasyMock.replay(kundeDAOMock);
//Eigentlichen Test durchführen
kundeService.leseAlleKunden();
//Prüfen, dass alle erwarteten Methoden am Mock-Objekt aufgerufen wurden.
EasyMock.verify(kundeDAOMock);
}
Alternatives
As an alternative to EasyMock, the following program libraries exist , which also allow the dynamic creation of mock objects:
literature
- David Astels: Test Driven Development: A Practical Guide . Prentice Hall International, 2003, ISBN 0-13-101649-0 .
- Vincent Massol, Ted Husted: JUnit in Action . Manning, 2003, ISBN 1-930110-99-5 .
- Johannes Link: Software tests with JUnit . Dpunkt Verlag, 2005, ISBN 3-89864-325-5 .
- Michael Hüttermann: Agile Java development in practice . O'Reilly, 2007, ISBN 3-89721-482-2 .
Web links
- EasyMock Homepage (English)
- OOPSLA (English)
Individual evidence
- ↑ Release 4.2 . January 29, 2020 (accessed January 30, 2020).
- ↑ EasyMock homepage .