09 - Automating JUnit

JUnit is used to automate unit and regression testing. Some advantages of using automated JUnit tests are:

  • efficient verification of bug fixes
  • reduces bad fixes
  • consistent overview and analysis of test results
  • increases productivity and system quality
  • allocates more time to design better tests

9.1 Running JUnit with Ant and Maven

Install Apache Ant 1.9 version by downloading it from http://ant.apache.org/bindownload.cgi.

Set ANT_HOME classpath variable for Ant. Set the location for the ant folder, for example:

Set the environment variable:


Add to the path:


Check if the installation is successful with ant –version command in command line tool:


Create a new Dynamic Web Java project in Eclipse:




The JUnit library should be added to the project classpath. Right click on the project >Java BuildPath > Add libraries


Ant 1.7 version no longer requires having junit.jar in Ant's startup classpath even if ant-junit.jar is present there.

Add ANT_HOME directory and junit jar in Window > Preferences> Ant > Runtime > Ant Home Entries and Global entries:

Create a test class:

import static org.junit.Assert.*;

import org.junit.Test;

public class MyUnitTest {

    public void mytest()

In order to build the Java project by executing the Ant script we need to create the Ant build script manually. We have to follow the next steps:

Select File > Export or right click on the project name and click Export > Export:

Select General > Ant Buildfiles:

Select the project name in the next window Generate Ant Buildfiles. Leave the name of the build file as default. Uncheck the field Create target to compile project using Eclipse compiler.

The build.xml file will be generated under the project directory as follows:


Double click on the buld.xml file to open its content in the Ant Editor of Eclipse:

In External Tools Configuration (Targets) the tasks can be selected or ordered:


The result in the console is:


Buildfile: C:\Users\vtg9234\workspace\AntJUnitSample\build.xml
     [echo] AntJUnitSample: C:\Users\vtg9234\workspace\AntJUnitSample\build.xml
    [junit] Running test.MyUnitTest
    [junit] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0,041 sec
[junitreport] Processing C:\Users\vtg9234\workspace\AntJUnitSample\junit\TESTS-TestSuites.xml to C:\Users\vtg9234\AppData\Local\Temp\null1010150205
[junitreport] Loading stylesheet jar:file:/C:/ant/apache-ant-1.9.4/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform time: 313ms
[junitreport] Deleting: C:\Users\vtg9234\AppData\Local\Temp\null1010150205
Total time: 1 second

Open a web report in browser – index.html generated in junit folder and look at the generated report:

If we want to try to run in Eclipse JUnit tests with Maven we have to create a Maven Project:


The project will have the following structure:


Add the same test class as in the example with Ant. Add Junit library in pom.xml


Then try to build the project without checking the option Skip tests.


The result from the console is:

[INFO] Scanning for projects...
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven JUnit Example 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ mavenjunitexample ---
[INFO] Deleting C:\Users\vtg9234\workspace\mavenjunitexample\target
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mavenjunitexample ---
 [INFO] Copying 0 resource
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ mavenjunitexample ---
[INFO] Nothing to compile - all classes are up to date
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mavenjunitexample ---

 [INFO] Copying 0 resource
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ mavenjunitexample ---

 [INFO] Compiling 1 source file to C:\Users\vtg9234\workspace\mavenjunitexample\target\test-classes
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mavenjunitexample ---
[INFO] Surefire report directory: C:\Users\vtg9234\workspace\mavenjunitexample\target\surefire-reports

 T E S T S
Running MyUnitTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.035 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mavenjunitexample ---
[INFO] Building jar: C:\Users\myuser\workspace\mavenjunitexample\target\mavenjunitexample-0.0.1-SNAPSHOT.jar
[INFO] --- maven-install-plugin:2.4:install (default-install) @ mavenjunitexample ---
[INFO] Installing C:\Users\myuser \workspace\mavenjunitexample\target\mavenjunitexample-0.0.1-SNAPSHOT.jar to C:\Users\myuser\.m2\repository\com\somecompany\myapp\mavenjunitexample\0.0.1-SNAPSHOT\mavenjunitexample-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\Users\myuser\workspace\mavenjunitexample\pom.xml to C:\Users\myuser\.m2\repository\com\somecompany\myapp\mavenjunitexample\0.0.1-SNAPSHOT\mavenjunitexample-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.360 s
[INFO] Finished at: 2015-01-31T01:51:06+02:00
[INFO] Final Memory: 10M/24M
[INFO] ------------------------------------------------------------------------

9.2 JUnit extensions

JUnit has been extended at the more frameworks. Some of these frameworks are:






Cactus is a simple test framework for unit testing server-side applications – Servlets, Ejb, etc. The scope of Cactus is to simplify and decrease the cost of writing tests. Cactus is projected as a container and the tests are executed inside this container.

Cactus extends JUnit and provide three specific junit.framework.TestCase subclasses:




import org.apache.cactus.*;
Import junit. framework. *;

    public class TestSampleServlet extends ServletTestCase {
       public void testServlet() {
          // Instantiate class to test
          ExampleServlet exampleservlet = new ExampleServlet();

          // Set a variable in session as the someMethod()
          // method that we are testing 
          session.setAttribute("name", "value");

          // Call the method to test, passing an 
          // HttpServletRequest object
          String result = servlet.someMethod(request);

          // Perform verification that test was successful
          assertEquals("some result", result);
          assertEquals("some Value", session.getAttribute("some Name"));

Cactus was previously hosted by Jakarta but was retired due to the lack of development community.


JWebUnit is a Java- based testing framework for web applications. It binds the testing frameworks HtmlUnit and Selenium.

JWebUnit operates on the top of JUnit. The test case class is JUnit4 Test Case.

import junit.framework.TestCase;
import net.sourceforge.jwebunit.WebTestCase;

public class SampleWebTestCase extends WebTestCase {

    public static void main(String[] args){
        junit.textui.TestRunner.run(new TestSuite(JWebUnitTest.class));
     //set base url
    public void setUp() throws Exception {     

   // test base info
   public void testSearchWord() {
       setFormElement("r", "searchValue");



Like us on Facebook