EasyMock Tutorial for Beginners – Learn EasyMock in simple and easy steps starting Environment Setup, First Application, JUnit Integration, Adding Behavior. EasyMock JUnit Integration – Learn EasyMock Programming in simple and easy steps starting from Overview, Environment Setup, First Application, JUnit. EasyMock is then used to emulate objects to enable Junit to test each object in isolation. You should be able to run tests with JUnit from Eclipse after reading this .

Author: Dobar Tanris
Country: Burundi
Language: English (Spanish)
Genre: Career
Published (Last): 12 July 2014
Pages: 234
PDF File Size: 9.95 Mb
ePub File Size: 5.59 Mb
ISBN: 300-8-85963-767-7
Downloads: 38636
Price: Free* [*Free Regsitration Required]
Uploader: Nalmaran

Developers always need to take care about code they produced. They should easymmock ensured that the code works properly after a new feature was implemented or some bug was fixed. That can be achieved with the help of unit tests.

Since this blog is dedicated to the Java programming language, today I will write an article about the JUnit 4. The main purpose of these frameworks is to make writing of unit tests easier. Junjt alternative you can read about JUnit and Mockito.

It should be very familiar for Java developers, because it emulates work of a coffee machine.

JUnit Testing Using EasyMock

Now I will describe in a few words functionality xnd the application. As you probably know, Coffee machine has two containers: Basically, coffee is available in three sizes: Now try to be more patient and attentive. As mentioned above, coffee machine can produce three sizes of coffee depending on portion you want to get. Portions in the application will be represented as an enumeration. Our coffee machine has two containers, that means we need to build logical architecture for them.

In order to avoid code duplication I need to develop an abstract class for the container. In context of this programming approach I want to bethink of one of my posts about Abstract class VS Interface.


Testing with EasyMock – Tutorial

Methods in the Abstract Container are self explained, so there is no need to stop on them in detail. After the container interface is developed and the abstract class is completed, we can proceed with a concrete container realization.

Now we have all required stuff to develop code related to the coffee machine. As previously done, I will start from interface development. Before I start JUnit tests development, I want to repeat canonical aims of unit tests. A unit test checks the smallest part of functionality — a method or a class. This circumstance imposes some logical restrictions on development. And one more important thing — unit testing implies isolation of functionality from other parts of the application.

For this purpose we need to add some dependencies to the pom. I have chosen the AbstractContainer class for the demonstration of JUnit tests, because in context of the application we have two realizations of this class.

If we write tests for it, we will automaticaly test the WaterContainer class and the CoffeeContainer class. I need to explain for what purpose all the annotations are used. There you can read the most correct explanations. Assertions are an essential part of each test, because all manipulations in a test should be checked in the end of it.

Ok, in the previous paragraph I showed you the example of several simple JUnit tests. What if we need to involve some extra class in a JUnit test? For this purpose you can use the EasyMock testing framework. With the help of EasyMock you can crate mocks. Mocks are objects which emulate behaviour a of real concrete object, but with one big plus — you can specify state for the mock, and in this way you obtain that state for the fake object which you need at a particular moment of unit test.


In the previous code snippet you see interaction of JUnit and EasyMock. I can underline several fundamental things in EasyMock usage.

If test requires interaction with some external object you should mock it. Set behavior for a mock or for a concrete method which is required for the testing of object under easympck test. EasyMock has a lot of different methods its API is not small, so I recommend you read more on the official site.

When you have a small application, you can launch JUnit tests separately, but what if you work on a large and complex application?

EasyMock Tutorial

In this case unit tests can be aggregated in test suits by some feature. JUnit provides convenient way for this. Unit testing is a very important part of software development, it has a lot of approaches, methodologies and tools. In this post I have made an overview of JUnit and EasyMock, but I omitted many interesting moments and techniques which I plan to cover in the following tutorials.

You can download the source code of the tutorial from my DropBox. Mathematician, programmer, wrestler, last action hero Application code Now easymick to be more patient and attentive.

Switch the mock to a reply mode. JUnit test suite When you have a small application, you can launch JUnit tests separately, but what if you work on a large and complex application?

Alex Fruzenshtein Mathematician, programmer, wrestler, last action hero Close About me Contacts Become an author.