Friday, July 23, 2010

Unit Testing

Unit testing is a part of the software development life cycle. Whenever a developer writes a piece of code say A  for a product, he needs to write another piece of code to make sure that the code A behaves as per his expectations.This ensures that before the code is shipped with the product,  its throughly tested and the bugs in the code are caught at an early stage of development. This helps in keeping the maintenance cost to minimum.

Pure Unit Test -
Technically speaking, unit testing is basically about testing the piece of code you have implemented in isolation.

This means that you should be able to test only the code which you have implemented without testing any code which your code is dependent on. The dependencies of the code to be tested should be injectable. Having injectable dependencies will help you create mock dependencies instead of real ones which you can then plug into the code to be tested when you are running the unit test cases.

Unit Testing Framework -
Junit is an open source unit testing framework developed for testing a piece of code written in the java language. Its one of the simplest to understand framework for the ones who have never written unit test cases in Java. Also the version 4 of  Junit supports the use of annotations which simplifies and speeds up your testing process.

more info @ http://www.junit.org/.

Mocking Framework -
EasyMock is also  open source framework which allows you to create mock objects on the fly without having you to actually write a mock class. This is a very powerful framework which drastically helps you to speed up on the testing although with a learning curve.

By default, EasyMock supports mocking of interfaces only. In order to mock classes, you will need to use the classextensions library of the EasyMock framework. Also EasyMock only supports mocking of non static methods. In order to mock static methods you may consider using PowerMock which is build on top of EasyMock.

You can find more information regarding
 EasyMock    @ http://easymock.org/  and
 PowerMock @ http://code.google.com/p/powermock/.

In future, I will be posting source code showing examples of mocking using EasyMock.

No comments: