Test Driven Development: JUNIT TO THE RESCUE !

INTRODUCTION TO JUNIT OR TEST DRIVEN DEVELOPMENT

The day I came across JUNIT, I was really fascinated by the APIs it provided. Writing Test Cases will make your brain to stress a bit more at the start, but with time passes by it will change the way you write your production code.
Unit testing and test driven development are proven ways to improve both the productivity of a developer and the quality of their software. JUnit IS the predominant choice for testing tool in the Java space.
There are two versions of JUnit, version 3 and version 4, which are radically different. JUnit 4 uses the annotation feature (since JDK 1.5) to streamline the process and drop the strict naming conventions.

STEPS TO WRITE A TDD 

  1. RED: Write a Test which fails.
  2. GREEN: Write simplest possible code to make it pass.
  3. REFACTOR: Change the structure of both Test and Production code without affecting the functionality.

KEEP IT GREEN

RULES FOR TDD

  1. No production code without failing test.
  2. Just enough test to cause code to fail.
  3. Just enough code to make test pass.

RULES FOR WRITING A TEST CASE:

  • It must be public.
  • It must return void.
  • The name must begin with “test”. (Optional which I don’t prefer )

Life Cycle of JUNIT

 Stage  Method Description
 Setup  Called once before a test. Instantiating objects and inserting test data into a  database.
 Test  Each test methods are called once which includes all the testing logic.
 Teardown  Called once in the end, for closing database connections etc.

Assertions

 Assertion  Functionality
 assertNull(Object x)  Validates that the parameter is null.
 assertNotNull(Object x)  Validates that the parameter is not null.
 assertTrue(boolean x)  Validates that the parameter is true.
 assertEquals(Object x, Object y)  Validates that the two objects passed are equal based on the .equals(Object obj1,  Object obj2) method.
 assertSame(Object x, Object y)  Validates that the two objects passed are equal based on the == operator.
 assertNotSame(Object x,Object y)  Validates that the two objects passed are not equal based on the == operator.
 fail()  Programmatically fail the test.

Annotations Instead Of Extending TestCase:

  1. @After None Method will be executed after each test method (similar to the tearDown() method in JUnit 3.x). Multiple methods may be tagged with the @After annotation, however no order is guaranteed.
  2. @AfterClass None Method will be executed after all of the test methods and teardown methods have been executed within the class. Multiple methods may be tagged with the @AfterClass annotation, however no order is guaranteed.
  3. @Before None Method will be executed before each test method (similar to the setUp() method in JUnit 3.x). Multiple methods may be tagged with the @Before annotation, however no order is guaranteed.
  4. @BeforeClass None Executed before any other methods are executed within the class. Multiple methods may be tagged with the @BeforeClass annotation, however no order is guaranteed.
  5. @Ignore String (optional) Used to temporarily exclude a test method from test execution. Accepts an optional String reason parameter.
  6. @Parameters None Indicates a method that will return a Collection of objects that match the parameters for an available constructor in your test. This is used for parameter driven tests.
  7. @RunWith Class Used to tell JUnit the class to use as the test runner. The parameter must implement the interface junit.runner.Runner.
  8. @SuiteClasses Class [] Tells JUnit a collection of classes to run. Used with the @RunWith(Suite.class) annotation is used.
  9. @Test { Class(optional) , Timeout(optional)} Used to indicate a test method. Same functionality as naming a method public void testXYZ() in JUnit 3.x. The class parameter is used to indicate an exception is expected to be thrown and what the exception is. The timeout parameter specifies in milliseconds how long to allow a single test to run. If the test takes longer than the timeout, it will be considered a failure.

Examples Code can be forked here & here

Advice to lookout : This guy(TDD) needs lots and lots of time, patience is the main ingredient while learning TDD.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s