Pages

Wednesday, October 24, 2012

Unit testing - Java JUnit 3

Download and build JUnit

Download the source from git, and execute ant.

The result jar file is compiled into junit4.11-SNAPSHOT directory.

Simple example

Class under test

public class Hello
{
    int add(int x, int y)
    {
        return (x + y);
    }
    
    int sub(int x, int y)
    {
        return (x - y);
    }
}

Test class

import junit.framework.*;
import static org.junit.Assert.assertEquals;

public class HelloTest extends TestCase {
  private Hello tester = null;
  protected void setUp() {
       tester = new Hello();
  }

  public void testAdd() {
    assertEquals("Result1", 15, tester.add(10, 5));
  }
}
TestCase comes from junit.framework.*, And you need to have '.' for classpath in order to find the class under the test. You need to copy the junit.jar in the classpath also.

Compile and execute

The junit.textui.TestRunner is called with the test class (HelloTest) parameter.
javac Hello.java
javac -cp .:junit-4.11-SNAPSHOT.jar HelloTest.java
java -cp .:junit-4.11-SNAPSHOT.jar junit.textui.TestRunner HelloTest
.
Time: 0.001

OK (1 test)

setUp() added

You can add setup() and tearDown() method.

import junit.framework.*;
import static org.junit.Assert.assertEquals;

public class Hello2Test extends TestCase {
  private Hello tester = null;
  protected void setUp() {
       super.setup();
       tester = new Hello();
  }

  public void testSub2() {
    assertEquals("Result1", 15, tester.sub(20, 5));
  }
  
  public void testSub() {
    assertEquals("Result1", 15, tester.sub(20, 5));
  }
}

Or, you can use @Override
  @Override
  protected void setUp() {
       tester = new Hello();
  }

Using suite()

You can make suite(), and add test methods to the suite.
import junit.framework.*;
import static org.junit.Assert.assertEquals;

public class Hello2Test extends TestCase {
  private Hello tester = null;
  
  public Hello2Test(String str)
  {
      super(str);
  }
  
  @Override
  protected void setUp() {
    tester = new Hello();
  }

  public void testAdd() {
    assertEquals("Result1", 15, tester.add(10, 5));
  }
  
  public void testAdd2() {
    assertEquals("Result2", 25, tester.add(10, 15));
  }
  
  public static Test suite() {
      TestSuite suite= new TestSuite();
      suite.addTest(
          new Hello2Test("testAdd") {
               protected void runTest() { testAdd(); }
          }
      );

      suite.addTest(
          new Hello2Test("testAdd2") {
               protected void runTest() { testAdd2(); }
          }
      );
      return suite;
  }
  
  public static void main(String[] args) {
      junit.textui.TestRunner.run(suite());
  }
}

You can use dynamic way to put all the tests in the suite using one line of code with Java Reflection working behind the scene.
  public static Test suite() {
      // method 1
      return new TestSuite(HelloTest.class);
  }
You can just execute the test like normal java code
javac Hello.java
javac -cp .:junit-4.11-SNAPSHOT.jar HelloTest.java
java -cp .:junit-4.11-SNAPSHOT.jar HelloTest

Merging tests with suite()

You can collect tests from TestCases to make test suite() to run.
import junit.framework.*;

public class HelloTestSuite
{
    public static Test suite() { 
        TestSuite suite= new TestSuite(); 
        suite.addTest(new HelloTest("testAdd")); 
        suite.addTest(new Hello2Test("testAdd")); 
        suite.addTest(new Hello2Test("testAdd2")); 
        return suite;
    }
    
    public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
    }
}
One can even concatenate the test cases into suite.
public class HelloTestSuite
{
    public static Test suite() { 
        TestSuite suite= new TestSuite(); 
        suite.addTestSuite(HelloTest.class); 
        suite.addTestSuite(Hello2Test.class); 
        return suite;
    }
    
    public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
    }
}

Reference

  1. vogella.com on JUnit
  2. JUnit Testing Utility Tutorial
  3. JUnit 4.0 Example

No comments:

Post a Comment