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 developed by Kent Beck and Erich Gamma. <bib>maldonado-vincenzi:2007</bib>
- JUnit was created at 1994. <bib>maldonado-vincenzi:2007</bib>
- JUnit is hosted at http://www.junit.org/ and http://sourceforge.net/projects/junit/. <bib>maldonado-vincenzi:2007</bib>
- JUnit only records failed assertions.
- Instead of using Java's default assertion mechanism, one can use JUnit's provided assertions.
- JUnit implements several assertions in the class
org.junit.Assert
: assertThat, assertArrayEquals, assertEquals, assertSame, assertNotSame, assertTrue, assertFalse, assertNull, assertNotNull, and fail. <bib>maldonado-vincenzi:2007</bib>
- 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>
- JUnit requires the Java Development Kit version 1.5 or newer.
- 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.
- 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>
- 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>
- You can add the library to the CLASSPATH environment variable.
- To execute the JUnit application, you must add the JUnit library (junit-4.8.1.jar) to the Java Classpath.
- Shake down <bib>maldonado-vincenzi:2007</bib>
- 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
- The class with all the test cases for JUnit is
- To check whether JUnit was correctly installed, you can run the JUnit test suite
- Compilation <bib>maldonado-vincenzi:2007</bib>
- 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
andorg.junit.Assert
.- javac -cp /opt/junit-4.8.1/junit-4.8.1.jar <test case class>
- 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
- Execution using command line runner <bib>maldonado-vincenzi:2007</bib>
- JUnit Runner is implemented by the class
org.junit.runner.JUnitCore
- 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>
- JUnit Runner is implemented by the class