JUnit

Concepts

  • JUnit is an open-source framework that provides support for documenting and automating the execution of test sets for Java programs. <bib>maldonado-vincenzi:2007</bib>


Facts

  • JUnit provides a test driver for the product under testing, which is implemented by JUnit test cases. <bib>vincenzi-etal:2007</bib>
  • JUnit provides facilities for automating the test execution, such as JUnit runners. <bib>vincenzi-etal:2007</bib>
  • JUnit was created at 1994. <bib>maldonado-vincenzi:2007</bib>
  • Instead of using Java's default assertion mechanism, one can use JUnit's provided assertions.
  • JUnit is able to choose subsets of tests according to a user-generated classification scheme defined using Junit categories.


Procedures

  • Installation <bib>maldonado-vincenzi:2007</bib>
    1. JUnit requires the Java Development Kit version 1.5 or newer.
    2. Download JUnit at http://sourceforge.net/projects/junit/.
      • Current version is 4.8.1.
      • The application is distributed as a JAR file (comprised of just the JUnit library) and a compressed ZIP file (with the JUnit library and documentation).
      • Download the ZIP file.
    3. Uncompress the file on a given directory that you have written permission.
      • mkdir /opt/junit-4.8.1
      • unzip junit4.8.1.zip -d /opt/junit-4.8.1


  • Command line configuration <bib>maldonado-vincenzi:2007</bib>
    1. To execute the JUnit application, you must add the JUnit library (junit-4.8.1.jar) to the Java Classpath.
      • You can add the library to the CLASSPATH environment variable.
        • At Unix: export CLASSPATH="/opt/junit-4.8.1/junit-4.8.1.jar:$CLASSPATH"
        • At Windows: set CLASSPATH="C:\junit-4.8.1\junit-4.8.1.jar;%CLASSPATH%"
      • You can use the -cp option when running the tests. This is the recommended option!
        • java -cp /opt/junit-4.8.1/junit-4.8.1.jar <program>


  • Shake down <bib>maldonado-vincenzi:2007</bib>
    1. To check whether JUnit was correctly installed, you can run the JUnit test suite
      • The class with all the test cases for JUnit is org.junit.tests.AllTests
      • This class is located at the root of JUnit installation directory.
      • java -cp /opt/junit-4.8.1/junit-4.8.1.jar:/opt/junit-4.8.1 org.junit.runner.JUnitCore org.junit.tests.AllTests


  • Compilation <bib>maldonado-vincenzi:2007</bib>
    1. To compile a JUnit test case, it is necessary to include in the Java Classpath the path to JUnit library (junit-4.8.1.jar) since some classes of the JUnit are used, such as org.junit.Test and org.junit.Assert.
      • javac -cp /opt/junit-4.8.1/junit-4.8.1.jar <test case class>


  • Execution using command line runner <bib>maldonado-vincenzi:2007</bib>
    1. JUnit Runner is implemented by the class org.junit.runner.JUnitCore
    2. To execute a class that implements JUnit test cases, execute the runner class and supply, as argument, the class that contains the test cases.
      • javac -cp /opt/junit-4.8.1/junit-4.8.1.jar org.junit.runner.JUnitCore <test case class>