Categories: Ant

Ant and jUnit Integration and Generating jUnit Report

Here we will show you how to run a junit test in Ant build. Also show you Ant and jUnit Integration into Ant build file and generating jUnit Report using ANT build file. You can run your JUnit unit tests with Apache Ant – an open source build tool.

Ant and jUnit Integration and Generating jUnit Report

JUnit is the commonly used unit testing framework for Java-based developments. It is easy to use and easy to extend.

Ant has a built-in task called “junit” which can run your unit tests. Here is a simple example:

<target name="unittest">
  <mkdir dir="reports"/>
  <mkdir dir="reports/raw"/>
  <mkdir dir="reports/html/"/>
    <junit printsummary="yes" haltonfailure="yes" showoutput="yes">
      <classpath>
       <pathelement location="${build.dir}/classes"/>
             <fileset dir="${basedir}">
                 <include name="lib/*.jar" />
             </fileset>
         </classpath>
          <test name="com.dineshonjava.algo.AppTestCases" todir="reports/raw"/>
      <formatter type="xml"/>
    </junit>
 </target>
 

In the classpath element I add the output locations of my project code and junit jars, and test code. In other words, the directories where my compiled code + test code is located.

In the test element I declare which unit test class to execute. I also specify the name of the output report in the todir attribute (“reports/raw”).

Now jUnit test with report generation in the HTML.

<target name="unittest">
  <mkdir dir="reports"/>
  <mkdir dir="reports/raw"/>
  <mkdir dir="reports/html/"/>
    <junit printsummary="yes" haltonfailure="yes" showoutput="yes">
      <classpath>
       <pathelement location="${build.dir}/classes"/>
             <fileset dir="${basedir}">
                 <include name="lib/*.jar" />
             </fileset>
         </classpath>
          <test name="com.dineshonjava.algo.AppTestCases" todir="reports/raw"/>
      <formatter type="xml"/>
    </junit>
 </target>
 
 <target name="test-reports" depends="unittest">
        <junitreport todir="reports">
            <fileset dir="reports/raw/">
                <include name="TEST-*.xml" />
            </fileset>
            <report format="noframes" todir="reports/html/" />
        </junitreport>
    </target>

Here second target “test-report” generate the report in the form of HTML to the given location in todir attribute “reports/html”.

Following file is our test class with some test methods.

AppTestCases.java

/**
 * 
 */
package com.dineshonjava.algo;

import static org.junit.Assert.*;

import org.junit.Test;

/**
 * @author Dinesh.Rajput
 *
 */
public class AppTestCases {

 @Test
 public void add() {
  assertEquals(5, 3+2);
 }
 
 @Test
 public void subs() {
  assertEquals(1, 3-2);
 }
 
 @Test
 public void mult() {
  assertEquals(6, 3*2);
 }

}

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="AlgoTest.makejar" default="makejar" basedir=".">
 <presetdef name="javac">
  <javac includeantruntime="false"/> 
 </presetdef>
  <property name="build.dir" value="build" />
  <target name="clean" description="cleans up the build by deleting the build,dist, web directories">
     <delete dir="${build.dir}"/>
   </target>
   <target name="init" depends="clean" description="Setup for build script">   
     <mkdir dir="${build.dir}"/>
   </target>
   <target name="compile" depends="init" description="Compiles .java files to WAR directory">
     <mkdir dir="${build.dir}/classes"/>
       <javac srcdir="src" destdir="${build.dir}/classes">
        <classpath>
            <fileset dir="${basedir}">
                <include name="lib/*.jar" />
            </fileset>
        </classpath>
       </javac>
     </target>    
      <target name ="makejar" description="Create a jar for the AlgoTest project" depends="compile">
     <jar jarfile="${build.dir}/AlgoTest.jar" basedir="${build.dir}/classes">
      <manifest>
        <attribute name = "Main-Class" value = "com.dineshonjava.algo.Test"/>
     </manifest>
     </jar>
   </target>
 <target name="run">
  <java jar="${build.dir}/AlgoTest.jar" fork="true"></java>
 </target>
 <target name = "generate-javadoc">
  <mkdir dir="doc"/>
   <javadoc packagenames="com.dineshonjava.algo.*" sourcepath="src" 
        destdir = "doc" version = "true" windowtitle = "Algo Application">
     <doctitle><![CDATA[= Algo Application =]]></doctitle>
         <bottom>
            <![CDATA[Copyright © 2016. All Rights Reserved.]]>
         </bottom>
         <group title = "algo packages" packages = "com.dineshonjava.algo.*"/>
  </javadoc>
  <echo message = "java doc has been generated!" />
 </target>
 
 <target name="unittest" depends="compile">
  <mkdir dir="reports"/>
  <mkdir dir="reports/raw"/>
  <mkdir dir="reports/html/"/>
    <junit printsummary="yes" haltonfailure="yes" showoutput="yes">
      <classpath>
       <pathelement location="${build.dir}/classes"/>
             <fileset dir="${basedir}">
                 <include name="lib/*.jar" />
             </fileset>
         </classpath>
          <test name="com.dineshonjava.algo.AppTestCases" todir="reports/raw"/>
      <formatter type="xml"/>
    </junit>
 </target>
 
 <target name="test-reports" depends="unittest">
        <junitreport todir="reports">
            <fileset dir="reports/raw/">
                <include name="TEST-*.xml" />
            </fileset>
            <report format="noframes" todir="reports/html/" />
        </junitreport>
    </target>
 
</project>

This example shows the execution of JUnit on the com.dineshonjava.algo.AppTestCases junit class. Running the above code produces the following output:

After running this test cases please go to directory /report/html and we observe one html file is there now open this html file into any browser and look into the output as below.

For this working codes and whole project you could checkout from here

https://github.com/DOJ-SoftwareConsultant/AntAndJunitTestReport

 

Previous
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago