Sunday, March 21, 2010

JUnit

As requested posting a blog about Junit

What is Junit ?
Simple framework for writing and running automated tests. Includes useful abstractions for running tests including test cases, text fixtures and test suites.

package junit.samples;


import junit.frameworks.*;
import java.util.Vector;
import junit.extensions.*;


/**
* sample test for java.util.Vector
*/
public class VectorTest extends TestCase {
  protected Vector fEmpty;
  protected Vector fFull;


public VectorTest(String name) {
      super(name);
}
public static void main (String[] args) { 
      junit.textui.TestRunner.run (suite());
}
...
protected void setUp() {
  fEmpty = newVector();
  fFull = new Vector();
  fFull.addElement(new Integer(1));
  fFull.addElement(new Integer(2));
  fFull.addElement(new Integer(3));
}
public static Test suite() {
    return new TestSuite(VectorTest.class);
}
public void testCapacity() {
       int size= fFull.size();
       for (int i=0; i < 100; i++)
          fFull.addElement(new Integer(i));
       assertTrue(fFull.size() == 100+size);
}
public void testClone() {
   Vector clone =  (Vector)fFull.clone();
   assertTrue(clone.size() == fFull.size());
   assertTrue(clone.contains(new Integer(1)));
}
}
...


What is JUnitPerf ?
Collection of JUnit tests decorators used to measure the performance and scalability of functionality contained within existing JUnit tests. When ever performance and scalability requirements are critical, JUnit perf plays an pivotal role.

Supported JUnit test decorators:

Timed tests: Runs a test and measures elapsed time

Load tests: Runs a test with concurrent users and iterations

Threaded tests: Runs a test in separate thread

JUnitEE: This is an extension to Junit. It allows tests to be executed inside an application server. Hence tests run in production environment.

Code Coverage Tools:

Emma - I have used Emma as well as our own version of Clover in my previous company. One can find all information about Emma at emma.sourceforge.net

Clover is commercial, free to open source project. Here is an example image for a cal app:


No comments:

Post a Comment