Let's assume we have 2 Java files that we have written "Calculator.java" and "MyUtil.java" .
File: "Calculator.java"
public class Calculator
{
public int add( int number1, int number2)
{
return number1 + number2;
}
public int subtract( int number1, int number2)
{
return number1 - number2;
}
}
File: "MyUtil.java"
public class MyUtil
{
public String getFullName( String firstName, String lastName)
{
System.out.println( "MyUtil:" + firstName + lastName ) ;
return( firstName + " " + lastName) ;
}
}
As a developer I write a main function in each of the classes to test out the class methods.
File: "Calculator.java"
public class Calculator
{
public int add( int number1, int number2)
{
return number1 + number2;
}
public int subtract( int number1, int number2)
{
return number1 - number2;
}
public static void main( String args[] )
{
Calculator calculatorObject = new Calculator() ;
int result ;
result = calculatorObject.add( 2, 3 ) ;
if ( result != 5 )
System.out.println( "Error in add test." ) ;
result = calculatorObject.subtract( 4, 3 ) ;
if ( result != 1 )
System.out.println( "Error in subtract test." ) ;
}
}
File: "MyUtil.java"
public class MyUtil
{
public String getFullName( String firstName, String lastName)
{
System.out.println( "MyUtil:" + firstName + lastName ) ;
return( firstName + " " + lastName) ;
}
public static void main( String args[] )
{
MyUtil myUtilObject = new MyUtil() ;
String result = myUtilObject.getFullName( "Joe" , "Louis" ) ;
if (! result.equals( "Joe Louis" ) )
{
System.out.println( "Error in getFullName test." ) ;
}
}
}
There are a couple of problems with this approach. We have to run the 2 classes each time. If we had 100 classes we need to run each class manually. We are only creating a single object of the class to test a method and then reusing the same object. We should create separate objects to test each method. Also notice that we used "if" statements to do our comparisons JUnit has "asset equals" statements to help us .
Let's take out our testing code and try another approach.
File: "CalculatorTest.java"
import static org.junit.Assert.*;
//import static org.junit.Assert.assertEquals;
//import org.junit.Assert ;
import org.junit.Test;
public class CalculatorTest
{
public void testAdd()
{
Calculator calculatorObject = new Calculator() ;
int result ;
result = calculatorObject.add( 2, 3 ) ;
if ( result != 5 )
System.out.println( "Error in add test." ) ;
}
public void testSubtract()
{
Calculator calculatorObject = new Calculator() ;
int result ;
result = calculatorObject.add( 4, 3 ) ;
if ( result != 1 )
System.out.println( "Error in subtract test." ) ;
}
}
File: "MyUtilTest.java"
public class MyUtilTest
{
public void testGetFullName()
{
MyUtil myUtilObject = new MyUtil() ;
String result = myUtilObject.getFullName( "Joe" , "Louis" ) ;
if (! result.equals( "Joe Louis" ) )
{
System.out.println( "Error in getFullName test." ) ;
}
}
}
File: "TestRunner.java"
public class TestRunner
{
public static void main(String[] args)
{
Calculator calculatorObject = new Calculator() ;
int result ;
result = calculatorObject.add( 2, 3 ) ;
if ( result != 5 )
System.out.println( "Error in add test." ) ;
result = calculatorObject.subtract( 4, 3 ) ;
if ( result != 1 )
System.out.println( "Error in subtract test." ) ;
MyUtil myUtilObject = new MyUtil() ;
String result = myUtilObject.getFullName( "Joe" , "Louis" ) ;
if (! result.equals( "Joe Louis" ) )
{
System.out.println( "Error in getFullName test." ) ;
}
}
}
This is better since we only need to run 1 file "TestRunner.java" and we have our own test files "CalculatorTest.java and "MyUtilTest.java" . Let's see what we need to do if we add one ore method to the Calculator.java called "Multiply" . We add a test for it in the class "CalculatorTest.java" . We now have to update the file TestRunner.java . Notice that we don't have to do this in JUnit. In JUnit we just need the annotation "@Test" in front of a method. Using annotation is similar to adding configuration information and the JUnit framework takes care of handling the annotation.
For 2 or 3 files we can use our own scheme but if we are dealing with 200 or 300 files then work starts to add up. In regression testing our tests are going to run over and over again. The JUnit framework removes some of the grunt work and gives us features making it easier to write cases using annotations and helper functions. If we are using tools such as Maven, Gradle, Eclipse, Ant then all we have to do is add the test java file and do not even need to add a "TestRunner" class.