dcsimg
December 8, 2016
Hot Topics:

Building in J2EE Performance during the Development Phase

  • July 7, 2006
  • By Steven Haines
  • Send Email »
  • More Articles »

In addition, JUnit offers a fail() method that you can call anywhere in your test case to immediately mark a test as failing.

JUnit tests are executed by one of the TestRunner instances (there is one for command-line execution and one for a GUI execution), and each version implements the following steps:

  1. It opens your TestCase class instance.
  2. It uses reflection to discover all methods that start with “test”.
  3. It repeatedly calls setUp(), executes the test method, and calls teardown().

As an example, I have a set of classes that model data metrics. A metric contains a set of data points, where each data point represents an individual sample, such as the size of the heap at a given time. I purposely do not list the code for the metric or data point classes; rather, I list the JUnit tests. Recall that according to one of the tenets of Extreme Programming, we write test cases before writing code. Listing 1 shows the test case for the Metric class, and Listing 2 shows the test case for the DataPoint class.

Listing 1. DataPointTest.java

package com.javasrc.metric;

import junit.framework.TestCase;
import java.util.*;

/**
 * Tests the core functionality of a DataPoint
 */
public class DataPointTest extends TestCase
{
  /**
  * Maintains our reference DataPoint
  */
private DataPoint dp;

/**
 * Create a DataPoint for use in this test
 */
protected void setUp()
{
  dp = new DataPoint( new Date(), 5.0, 1.0, 10.0 );
}

/**
 * Clean up: do nothing for now
 */
protected void tearDown()
{
}

/**
 * Test the range of the DataPoint
 */
public void testRange()
{
  assertEquals( 9.0, dp.getRange(), 0.001 );
}

/**
 * See if the DataPoint scales properly
 */
public void testScale()
{
  dp.scale( 10.0 );
  assertEquals( 50.0, dp.getValue(), 0.001 );
  assertEquals( 10.0, dp.getMin(), 0.001 );
  assertEquals( 100.0, dp.getMax(), 0.001 );
}

/**
 * Try to add a new DataPoint to our existing one
 */
public void testAdd()
{
  DataPoint other = new DataPoint( new Date(), 4.0, 0.5, 20.0 );
  dp.add( other );
  assertEquals( 9.0, dp.getValue(), 0.001 );
  assertEquals( 0.5, dp.getMin(), 0.001 );
  assertEquals( 20.0, dp.getMax(), 0.001 );
}

/**
 * Test the compare functionality of our DataPoint to ensure that
 * when we construct Sets of DataPoints they are properly ordered
 */
public void testCompareTo()
{
  try
   {
    // Sleep for 100ms so we can be sure that the time of
    // the new data point is later than the first
    Thread.sleep( 100 );
  }
  catch( Exception e )
  {
  }

  // Construct a new DataPoint
  DataPoint other = new DataPoint( new Date(), 4.0, 0.5, 20.0 );

  // Should return -1 because other occurs after dp
  int result = dp.compareTo( other );
  assertEquals( -1, result );

  // Should return 1 because dp occurs before other
  result = other.compareTo( dp );
  assertEquals( 1, result );

  // Should return 0 because dp == dp
  result = dp.compareTo( dp );
  assertEquals( 0, result );
 }
}

Listing 2. MetricTest.java

package com.javasrc.metric;

import junit.framework.TestCase;
import java.util.*;

public class MetricTest extends TestCase
{
  private Metric sampleHeap;

  protected void setUp()
  {
    this.sampleHeap = new Metric( "Test Metric",
                                  "Value/Min/Max",
                                  "megabytes" );
    double heapValue = 100.0;
    double heapMin = 50.0;
    double heapMax = 150.0;

    for( int i=0; i<10; i++ )
    {
      DataPoint dp = new DataPoint( new Date(),
                                    heapValue,
                                    heapMin,
                                    heapMax );
      this.sampleHeap.addDataPoint( dp );
      try
      {
        Thread.sleep( 50 );
      }
      catch( Exception e )
      {
      }
      // Update the heap values
      heapMin -= 1.0;
      heapMax += 1.0;
      heapValue += 1.0;
    }
}

public void testMin()
{
  assertEquals( 41.0, this.sampleHeap.getMin(), 0.001 );
}

public void testMax()
{
  assertEquals( 159.0, this.sampleHeap.getMax(), 0.001 );
}

public void testAve()
{
  assertEquals( 104.5, this.sampleHeap.getAve(), 0.001 );
}

public void testMaxRange()
{
  assertEquals( 118.0, this.sampleHeap.getMaxRange(), 0.001 );
}

public void testRange()
{
  assertEquals( 118.0, this.sampleHeap.getRange(), 0.001 );
}

public void testSD()
{
  assertEquals( 3.03, this.sampleHeap.getStandardDeviation(), 0.01 );
}

public void testVariance()
{
  assertEquals( 9.17, this.sampleHeap.getVariance(), 0.01 );
}

public void testDataPointCount()
{
  assertEquals( 10, this.sampleHeap.getDataPoints().size() );
}
}

In Listing 1, you can see that the DataPoint class, in addition to maintaining the observed value for a point in time, supports minimum and maximum values for the time period, computes the range, and supports scaling and adding data points. The sample test case creates a DataPoint object in the setUp() method and then exercises each piece of functionality.

Listing 2 shows the test case for the Metric class. The Metric class aggregates the DataPoint objects and provides access to the collective minimum, maximum, average, range, standard deviation, and variance. In the setUp() method, the test creates a set of data points and builds the metric to contain them. Each subsequent test case uses this metric and validates values computed by hand to those computed by the Metric class.



Page 2 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel