EasyMock – JUnit Tests
May 7, 2010 at 3:54 pm Leave a comment
The aim of this article would be to provide myself with a documented version of my thoughts as I discover EasyMock. That would be the primary aim of this article. The secondary aim would be to provide anyone on this journey of discovery a view from someone who is just learning this technology.
A key component to any piece of code is its corresponding unit test. Let us start with my problem.
The Problem
I created a few tables using hibernate, this involved setting up the .hbm.xml mapping file as well as corresponding java classes. A unit test was created per table to confirm that each table can be accessed correctly. That is all fine, but the project I am currently on uses the Model View Controller (MVC) framework, this beautiful framework splits our design into the following:
1. Database
2. Business Logic
3. Front-End
Ok, we have completed the Database layer, now we dealing with the business logic. The main aim of the business logic would be to provide the front-end with data from the database. Let us assume we created a class that allows us to get the data from the database (rather since we using test driven development, created the interfaces and then create our unit test). So you are wondering, where does EasyMock come in? Well I never knew either, but after having a few discussions with others and reading up:
http://easymock.org/EasyMock2_2_Documentation.html
http://www.michaelminella.com/testing/unit-testing-with-junit-and-easymock.html
EasyMock 2 is a library that provides an easy way to use Mock Objects for given interfaces
So you ask what is the benefit of this?
- Reduces the need to actually access the database, thereby reducing the time it takes to run the unit test
- Guaranteed return values, otherwise an error is displayed
To me that are the basic and very important benefits.
How does it work?
EasyMock allows you to set up a “recording” then actually “running” then “verifying” the recording against the run.
That is my basic introduction into EasyMock, let me get into some more examples.
Blog soon!
Entry filed under: Testing. Tags: .
Trackback this post | Subscribe to the comments via RSS Feed