Showing posts with label Unit Testing. Show all posts
Showing posts with label Unit Testing. Show all posts

JUnit is the de facto standard unit testing library for the Java™ language. JUnit 4 is the first significant release of this library in almost three years. It promises to simplify testing by exploiting Java 5's annotation feature to identify tests rather than relying on subclassing, reflection, and naming conventions. In this article, obsessive code tester Elliotte Harold takes JUnit 4 out for a spin and details how to use the new framework in your own work. Note that this article assumes prior experience with JUnit.

Home of JUnit, Articles and Guides: PDF Guide, JavaDoc, Tutorials

Writing Unit test cases is now mandatory for every developers in the Companies. But at the same time it is very important that we need to analyze the Unit test coverage that is done by that developer. Just writing some test cases will not make sense. Here comes the job of Cobertura.

Cobertura is an open source tool used to analyze the Coverage of Unit tests. It will give Percentage values for the Line coverage as well as Branch coverage.

Some features  are listed below.

  • Can be executed from ant or from the command line.
  • In maven case we have Corbertura Maven plugins.
  • Instruments Java bytecode after it has been compiled.
  • Can generate reports in HTML or XML.
  • Shows the percentage of lines and branches covered for each class, each package, and for the overall project.
  • Shows the McCabe cyclomatic code complexity of each class, and the average cyclomatic code complexity for each package, and for the overall product.
  • Can sort HTML results by class name, percent of lines covered, percent of branches covered, etc. And can sort in ascending or decending order.

Screenshots:

Coverage

Unit testing is now a "best practice" for software development. In this unit testing we have to face so many situations where we need to interact with Database or any other resources. But at the same time we need to make our Tests isolated too. Here comes the importance of Mock objects.

Mock objects are a useful way to write unit tests for objects that act as mediators. “Instead of calling the real domain objects, the tested object calls a mock domain object that merely asserts that the correct methods were called, with the expected parameters, in the correct order.”
Using EasyMock Framework
EasyMock is a framework for creating mock objects using thejava.lang.reflect.Proxy object. When a mock object is created, a proxy object takes the place of the real object. The proxy object gets its definition from the interface or class you pass when creating the mock.
EasyMock is providing two APIs for creating mock objects that are based on interfaces, the other on classes (org.easymock.EasyMock and org.easymock. classextensions.EasyMock respectively).
We can separate the EasyMock implementation into FOUR steps
1. Creating a Mock Object using “EasyMock.createMock”.
Create Mock : Using this static method we are creating a mock object. And this is the first step which we need to do in mock testing.
When we creates this mock objects then we can follow three levels.
Regular: If we are expecting some methods to be executed then it was not executed then the test will fail.  And if any unexpected tests executed then also test will fail. Here the order of the method execution is not important.
Ex: EmpDAO empDAO = EasyMock.createMock(EmpDAO.class);
Nice: If we are expecting some methods to be executed then it was not executed then the test will fail. And if any unexpected tests executed then it will return a default value. Here also order is not important.
Ex: EmpDAO empDAO = EasyMock.createNiceMock(EmpDAO.class);
Strict: Same as regular but here the Order of the expected methods also important.
Ex: EmpDAO empDAO = EasyMock.createStrictMock(EmpDAO.class);
2. Expecting mock object method calls using “EasyMock.expect”.
This is used to expect some method calls from our mock object. Lets go through one example.
Lets assume we have the following methods which gets employee information from DB.
List<Employee> employee = empDao.getEmpDetails();
List<Employee> employee = empDao.getEmpDetailsByName(“bond”);
In the unit test we need to follow as follows…
EmpDao mockDao = EaskMock.createMock(EmpDao.class);
Employee mockEmp = new Employee();
mockEmp.setEmpName(“bond”);
mockEmp.setEmpCode(“007”);

List<Employee> empList= new ArrayList<Employee>(1);
empList.add(mockEmp);

expect(mockDao.getEmpDetails()).andReturn(empList);
expect(mockDao.getEmpDetailsByName(“bond”)).andReturn(empList);
replay(mockDao);
3. Registering/replaying expected methods using “EasyMock.replay”.
Once the behavior of the mock objects has been recorded with expectations, the mock objects must be prepared to replay those expectations. We are using replay() method for this purpose. EaskMock will stop the expecting behavior once this method calls.
EasyMock.replay(mockDao);
4. Verifying the expected methods using “EasyMock.verify”.
Veirfying the mock expectations is the final step which we need to follow. This includes validating that all methods that were expected to be called were called and that any calls that were not expected are also noted.
EasyMock.verify(mock);
Easymock is providing more functionalities like “Matchers” etc for more unit testing flexibility. EasyMock has been the first dynamic Mock Object generator, relieving users of hand-writing Mock Objects, or generating code for them. It helps us to increase our testing coverage a lot.
Technorati Tags: unit testing,easy mock,mock testing,mock objects,java,testing
+