Best Practices – white-box unit tests!

If you are writing unit test for a class ABC.java, then your unit test file should be named ABCTest.java. i.e. it should be of the format <SourceFile>Test.java. So that the test indicates what is the source file that it’s testing.

  1. Suppose your Source file resides in src/com/tier1app/ABC.java, then test file should also be kept in the same package, but in test folder i.e.: test/com/tier1app/ABCTest.java. Packaging the Test files in the same package of the source files, gives the flexibility to access public, protected and package level APIs and member variables directly.
  2. Consider giving a meaningful names for the test methods in the Unit Test File. Name of the method should imply what the method is trying to do. Example stated below shows the meaningful method names.
  3. Each Method in the unit test file, should exercise just only one functionality. Don’t have one method test out multiple functionalities.
  4. Before each method, write a java doc – which states the steps executed in the method and the expected result after methods are executed. Please refer to example below.
  5. Instrument code coverage testing tools like clover – which would indicate what percentage of the source code is covered by the unit tests. You should target to attain 100% code coverage.
  6. MOST IMPORTANT: Write unit tests for all possible and probable scenarios. Please refer to the example below.

Example:

Below are the unit test methods that are written for a ItineraryCountFilter.java. This class constructor takes in a count as it’s argument. It exposes only one public method:

public Set<Itinerary> doFilter(Set<Itinerary> itineraries);

Suppose if count to the constructor is passed as “5”. Then this method would return the top 5 itineraries from the itenraries that are passed as input. Here are unit test methods and their Javadoc:

/**
*
* <p/>Steps:<br/>
* 1. Pass 5 itineraries.
* 2. Set Filter Count as 3.
* 3. Do Filtering
*
* <p/>Expected Result:<br/>
* Only top 3 itineraries should be returned.
*/
public void goGood() {
:
}

/**
* <p/>Steps:<br/>
* 1. Pass 5 itineraries.
* 2. Set Filter Count as 0.
* 3. Do Filtering
*
* <p/>Expected Result:<br/>
* No Itineraries should be returned
*
*/
@Test
public void filterCountZero() {
:
}

/**
* <p/>Steps:<br/>
* 1. Pass negative value as filtering count (i.e. -1)
*
* <p/>Expected Result:<br/>
* IllegalArgumentException should be thrown
*
*/
@Test
public void filterCountNegative() {
:
}

/**
* <p/>Steps:<br/>
* 1. Pass ‘null’ as argument.
*
* <p/>Expected Result:<br/>
* Null Should be returned as result. No Exceptions should be thrown.
*
*/
@Test
public void nullItineraries() {
:
}

/**
* <p/>Steps:<br/>
* 1. Pass empty Itineraries set
*
* <p/>Expected Result:<br/>
* Empty Itineraries should be returned.
*
*/
@Test
public void emptyItineraries() {
:
}

/**
* <p/>Steps:<br/>
*
* 1. Build Itineraries Set of size 5.
* 2. Set Filtering Count as 10.
* 3. Do the Filtering
*
* <p/>Expected Result:<br/>
*
* Filtered Itinerary Set should be returned. It’s size should be 5.
*
*/
@Test
public void lessThanFilterCount() {
:
}

/**
* <p/>Steps:<br/>
*
* Invoke same filter multiple times to filter different itineraries
*
* <p/>Expected Result:<br/>
* 1. Filtering should succeed
* 2. It shouldn’t result in any exception.
*/
@Test
public void sameFilterMultipleTimes() {
:
}

/**
* Invoke same filter <b>concurrently</b> multiple times.<br/>
*
* <p/>Steps:<br/>
*
* 1. Fire 10 threads to work on the same filter concurrently.
*
* <p/>Expected Result:<br/>
*
* 1. All the 10 threads filtering should succeed.
* 2. None of them should result in exception.
*
*/
@Test
public void sameFilterMultipleTimesConcurrently()
:
}

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

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: