1. Home
  2. Tutorials
  3. Java
  4. JUnit
Yolinux.com Tutorial

JUnit: Java unit test framework

JUnit is a Java test framework for classes. JUnit has integrated support for many Java IDEs and continuous integration systems like Jenkins.



Free Information Technology Magazines and Document Downloads
TradePub link image

JUnit Installation:

This tutorial assumes that you have Java installed on your system.
For more on installing Java and setting up your classpath see the YoLinux.com Java tutorial.

Download a prebuilt JUnit jar file: http://www.junit.org eg. junit-4.9b2.jar

Place the JUnit jar file where accessible: (examples)
  • Linux: /opt/java/lib/ or /usr/java/latest/lib/
  • MS/Windows: C:\Java\lib\ or C:\Program Files\Java\jdk1.6.0_26\lib\

Add the JUnit jar file to your Java class path (environment variable or set in ant build script).
In this tutorial we will be using an ANT build script.

Project Source Hierarchy:

The build path used in this example is as follows:

ProjectX/build.xml
        /src/com/megacorp/projx/util/Addition.java
        |                           /Multiply.java
        /test/src/com/megacorp/projx/JUnit/AllTests.java
        /test/src/com/megacorp/projx/util/Addition_Test.java
        |    |                           /Multiply_Test.java
        |    /htmlreports/index.html                           (generated by JUnit)
        |    |           /com/megacorp/projx/JUnit/            (unit test reports end up here)
        /    /data/TEST-com.megacorp.projx.JUnit.AllTests.xml  (generated by JUnit)
Notes:
  • JUnit can generate reports in various formats. HTML reports are directly viewable in a browser with links to the individual reports. XML output is often used as an iinterface with other systems like Jenkins.
  • The unit test source mimics the source tree and class names.

JUnit Example:

Example Java Class to Test:

File: src/com/megacorp/projx/util/Addition.java
package com.megacorp.projx.util;

import java.text.*;

public class Addition
{

    public static int twoValues(int x, int y)
    {
        return x + y;
    }
}

File: src/com/megacorp/projx/util/Multiply.java
package com.megacorp.projx.util;

import java.text.*;

public class Multiply
{

    public static int twoValues(int x, int y)
    {
        return x * y;
    }
}

JUnit unit test source:

  • Class to specify tests to run: AllTests.java
  • Classes to perform the unit tests:
    • Addition_Test.java
    • Multiply_Test.java
File: test/src/com/megacorp/projx/JUnit/AllTests.java
package com.megacorp.projx.JUnit;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

import com.megacorp.projx.util.Addition_Test;
import com.megacorp.projx.util.Multiply_Test;

@RunWith(Suite.class)
@Suite.SuiteClasses({Addition_Test.class,Multiply_Test.class})
public class AllTests
{
    // Empty, since the annotations include all of the necessary configuration
}

File: test/src/com/megacorp/projx/util/Addition_Test.java
package com.megacorp.projx.util;

import java.io.IOException;
import java.io.PrintWriter;
import junit.framework.TestCase;
import com.megacorp.projx.util.Addition;
import com.megacorp.projx.util.Multiply;

public class Addition_Test extends TestCase
{
    private int x = 0;
    private int y = 0;
 
    // Stuff to do before test
    protected void setUp()
    {
        x = 4;
        y = 5;
    }

    // Stuff to do after test
    protected void tearDown()
    {
        x = 0;
        y = 0;
    }

    public void testAddition()
    {
        System.out.println("Test use of Addition class");

        int z = Addition.twoValues(x,y);
        System.out.println("    Result: " + z);

        assertEquals(9,z);  // The test
    }
}

File: test/src/com/megacorp/projx/util/Multiply_Test.java
package com.megacorp.projx.util;

import java.io.IOException;
import java.io.PrintWriter;
import junit.framework.TestCase;
import com.megacorp.projx.util.Addition;
import com.megacorp.projx.util.Multiply;

public class Multiply_Test extends TestCase
{
    private int x = 0;
    private int y = 0;

    protected void setUp()
    {
        x = 4;
        y = 5;
    }

    protected void tearDown()
    {
        x = 0;
        y = 0;
    }

    public void testMultiply()
    {
        System.out.println("Test use of Multiply class");

        int z = Multiply.twoValues(x,y);
        System.out.println("    Result: " + z);

        assertEquals(20,z);
    }
}

Ant file to compile and run a JUnit test:

File: ./build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="projx" default="all" basedir=".">
    <description>Builds, tests, and runs projx</description>
    <property name="build.dir" value="./"/>
    <property name="src.dir" location="${build.dir}/src"/>
    <property name="test.dir" location="${build.dir}/test/src"/>
    <property name="test.htmlreports.dir" location="${build.dir}/test/htmlreports"/>
    <property name="test.data.dir" location="${build.dir}/test/data"/>
    <property name="junit.class.name" value="com.megacorp.projx.JUnit.AllTests"/>

    <!-- Classpath to find java packages -->
    <path id="classpath.base">
      <pathelement location="/usr/java/latest/lib/tools.jar" />
    </path>

    <!-- Classpath for tests to find src packages -->
    <path id="classpath.src">
      <pathelement location="src" />
    </path>

    <path id="classpath.junittest">
       <path refid="classpath.base" />
       <pathelement location="/opt/java/lib/junit-4.9b2.jar" />
       <pathelement location="${test.dir}" />
    </path>

    <target name="clean" description="Remove all .class files">
      <delete includeEmptyDirs="true" failonerror="false">
        <fileset dir="${src.dir}">
          <include name="**/*.class"/>
        </fileset>
        <fileset dir="${test.dir}">
          <include name="**/*.class"/>
        </fileset>
        <fileset dir="${test.htmlreports.dir}">
          <include name="**/*.txt"/>
          <include name="**/*.xml"/>
          <include name="**/*.html"/>
        </fileset>
      </delete>
    </target>

    <target name="compile">
      <javac srcdir="${src.dir}" destdir="${src.dir}" debug="true" includeAntRuntime="false">
        <classpath refid="classpath.base"/>
        <include name="**/*.java"/>
      </javac>
    </target>

    <target name="compile-test" depends="compile">
      <javac srcdir="${test.dir}" destdir="${test.dir}" debug="true" includes="${src.dir}" includeAntRuntime="false">
        <classpath refid="classpath.base"/>
        <classpath refid="classpath.src"/>
        <classpath refid="classpath.junittest"/>
        <include name="**/*.java"/>
      </javac>
    </target>

    <target name="test" depends="compile-test">
      <mkdir dir="${test.data.dir}"/>
      <mkdir dir="${test.htmlreports.dir}"/>
      <junit fork="no" haltonfailure="no" showoutput="yes" printsummary="true">
        <test name="${junit.class.name}" todir="${test.data.dir}"/>
        <formatter type="brief" usefile="false"/>
        <formatter type="xml"/>
        <classpath refid="classpath.base" />
        <classpath refid="classpath.src" />
        <classpath refid="classpath.junittest" />
      </junit>
      <junitreport todir="${test.htmlreports.dir}">
        <fileset dir="${test.data.dir}">
          <include name="TEST-*.xml"/>
        </fileset>
        <report format="frames" todir="${test.htmlreports.dir}"/>
      </junitreport>
    </target>

    <target name="all" depends="compile-test" />

</project>

Building and running unit tests:
  • Build: ant
  • Test: ant test
[bash JUnit]$ ant clean
Buildfile: /home/user1/JUnit/build.xml

clean:

BUILD SUCCESSFUL
Total time: 0 seconds
[bash JUnit]$ ant test
Buildfile: /home/user1/JUnit/build.xml

compile:
    [javac] Compiling 2 source files to /home/user1/JUnit/src

compile-test:
    [javac] Compiling 3 source files to /home/user1/JUnit/test/src

test:
    [junit] Running com.megacorp.projx.JUnit.AllTests
    [junit] Testsuite: com.megacorp.projx.JUnit.AllTests
    [junit] Test use of Addition class
    [junit]     Result: 9
    [junit] Test use of Multiply class
    [junit]     Result: 20
    [junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.019 sec
    [junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.019 sec
    [junit] 
    [junit] ------------- Standard Output ---------------
    [junit] Test use of Addition class
    [junit]     Result: 9
    [junit] Test use of Multiply class
    [junit]     Result: 20
    [junit] ------------- ---------------- ---------------
[junitreport] Processing /home/user1/JUnit/test/htmlreports/TESTS-TestSuites.xml to /tmp/null1813236
[junitreport] Loading stylesheet jar:file:/opt/apache-ant-1.8.2/lib/ant-junit.jar ! 
 /org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform time: 315ms
[junitreport] Deleting: /tmp/null1813236

BUILD SUCCESSFUL
Total time: 1 second

JUnit reporting:

JUnit HTML Results Report:
JUnit HTML report
View JUnit results with browser by opening file ./test/htmlreports/index.html

Continuous Integration with Jenkins:

Jenkins configuration to run JUnit:

Jenkins/Hudson JUnit configuration
Configuration

For more, see the YoLinux.com Tutorials Jenkins installation and configuration and Jenkins Plugins for Java

JUnit, Maven and Jenkins:

Using Maven to build your project and JUnit tests will define the directory paths used and the Jenkins configuration. Unlike Ant which explicitly directs the build and test process, Maven implicitly controls the process using its own conventions including the directory paths used to place your code.

ProjectX/pom.xml
        /src/main/java/com/megacorp/projx/util/Addition.java
        |                                     /Multiply.java
        /src/test/java/com/megacorp/projx/JUnit/AllTests.java
        /src/test/java/com/megacorp/projx/util/Addition_Test.java
        |    |                                /Multiply_Test.java

Note that Maven controls the build process with pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <name>JUnit Test Example POM</name>
    <groupId>com.megacorp.projectx</groupId>
    <artifactId>ProjectX</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20</version>
                <configuration>
                    <redirectTestOutputToFile>true</redirectTestOutputToFile>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.jar.plugin>2.3.2</maven.jar.plugin>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>4.12</junit.version>
    </properties>

    <dependencies>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>${junit.version}</version>
           <scope>test</scope>
       </dependency>
    </dependencies>

</project>

Maven has implicit targets which are steps in its "Lifecycle". The targets are executed in the following order:
Maven TargetDescription
validatevalidate the project is correct and all necessary information is available
compilecompile the source code of the project
testtest the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
packagetake the compiled code and package it in its distributable format, such as a JAR.
verifyrun any checks on results of integration tests to ensure quality criteria are met
installinstall the package into the local repository, for use as a dependency in other projects locally
deploydone in the build environment, copies the final package to the remote repository for sharing with other developers and projects.

The target is inclusive of all prior to it. Thus "mvn install" will execute targets alidate, compile, package, verify and install.

Build the project: mvn install

Note that Maven downloads all maven and build dependancies and will take a while on the first build (thus an internet connection will be required). Dependencies will be downloaded and stored in ~/m2/...

The results are a JAR file a JUnit results:
ProjectX/target/ProjectX-1.0.0.jar
        |target/surefire-reports/... xml (test results)

Jenkins Maven plug-in and Reporting:

Configure Jenkins:

Configure Jenkins to use your Maven installation. Select "Jenkins" + "Global Tool Configuration":

Configure Jenkins to use your Maven installation

Configure Build Project:

Use the Jenkins Maven plug-in rather than executing Maven as a shell command. The Maven plug-in is Maven aware and will post-process JUnit results for use with Jenkins. Select your Jenkins project and "Configure":

Configure Build Project

Configure Build Project JUnit reporting:

Test results will be put into the following directory structure:

Configure JUnit reporting

For more on Jenkins, see the YoLinux tutorial on Jenkins and Java

Links:

Books:

"JUnit in Action"
by Petar Tahchiev, Felipe Leme, Vincent Massol, Gary Gregory
ISBN # 1935182021, Manning Publications; Second Edition edition (August 4, 2010)

JUnit testing including J2EE. Covers JUnit framework and extensions.

Amazon.com
"Core Java 2, Volume 1: Fundamentals "
by Cay S. Horstmann, Gary Cornell
ISBN # 0132354764, Prentice Hall PTR 8th edition

The industry standard. Need I say more?

Amazon.com
"Core Java 2: Volume 2 Advanced Features "
by Cay S. Horstmann, Gary Cornell
ISBN # 0132354799, Prentice Hall PTR 8th edition

The industry standard. Need I say more?

Amazon.com

   

    Bookmark and Share

Advertisements