10 - Using JUnit in a java project: Page 3 of 3

10.3 Launching tests with test runners

Test runners are designed to execute the tests. JUnit distribution includes 3 Test Runner classes for:

  • text console
  • swing
  • AWT

There is no TestRunner interface. If a test runner is necessary for other reason the class TestRunner can be extended. In Cactus framework the BaseTestRunner class is extended to create ServletTestRunner in order to run JUnit test from a browser.

Running a test from the command line requires that the test class and tested class for example Calculator.java and CalculatorTest.java should be compiled together with the TestRunner class. 

Public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
    public int sub(int a, int b) {
        return a - b;
    }
}

import static org.junit.Assert.*;
import myapp.Calculator;
import org.junit.Test;

//test class calculator
public class CalculatorTest {

     Calculator calculator = new Calculator();
    @Test
      public void testSubstract() {
        assertEquals("substract", 8, calculator.add(5, 3));
      }     
      @Test
      public void testMultiply() {
        assertEquals("multiply", -1, calculator.sub(7, 8));
      }
}

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {
   public static void main(String[] args) {
      Result result = JUnitCore.runClasses(CalculatorTest.class);
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
      System.out.println(result.wasSuccessful());
   }
}

Run it from command line (see the other examples of sub-chapter 5.1):

C:\junit>javac -classpath "C:\junit\junit-4.11.jar";"C:\junit\junit-master/lib/hamcrest-core-1.3.jar"; Calculator.java CalculatorTest.java TestRunner.java

C:\junit>java -classpath "C:\junit\junit-4.11.jar";"C:\junit\junit-master\lib\hamcrest-core-1.3.jar"; TestRunner

If we want to run multiple test cases JUnit responds to this with TestSuite. (see sub – chapter 7.5 ).

A test case can specify its expected runner type with @RunWith annotation. If no type is specified the runtime chooses BlockJUnit4ClassRunner as default. The class is responsible that the each test case will run with a new test instance.

import static org.junit.Assert.*;
import myapp.Calculator;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(CalculatorRunner.class)
public class CalculatorTest {

     Calculator calculator = new Calculator();
     @Test
      public void testSubstract() {
        assertEquals("substract", 8, calculator.add(5, 3));
      }     
      @Test
      public void testMultiply() {
        assertEquals("multiply", -1, calculator.sub(7, 8));
      }
}

import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;

public class CalculatorRunner extends Runner {
   public static void main(String[] args) {
      Result result = JUnitCore.runClasses(CalculatorTest.class);
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
      System.out.println(result.wasSuccessful());
   }

   @Override
   public Description getDescription() {
    // TODO Auto-generated method stub
    return null;
   }
   @Override
   public void run(RunNotifier arg0) {
    // TODO Auto-generated method stub
    
   }
}

JUnit Runners are highly adaptable and let the developer change the test execution procedure and the whole test process.

Some examples of custom Runner implementations are:

  • SpringJUnit4ClassRunner for dependency injections in test classes for Spring framework applications
  • MockitoJUnitRunner for mock initialization

Like us on Facebook