English 中文(简体)
JUnit - Plug with Ant
  • 时间:2024-12-22

JUnit - Plug with ANT


Previous Page Next Page  

We will have an example to demonstrate how to run JUnit using ANT. Follow the steps given below.

Step 1: Download Apache Ant

Download Apache Ant based on the operating system you are working on.

OS Archive Name
Windows apache-ant-1.8.4-bin.zip
Linux apache-ant-1.8.4-bin.tar.gz
Mac apache-ant-1.8.4-bin.tar.gz

Step 2: Set Ant Environment

Set the ANT_HOME environment variable to point to the base directory location, where the ANT pbraries are stored on your machine. Let us assume the Ant pbraries are stored in the folder apache-ant-1.8.4.

Sr.No. OS & Description
1

Windows

Set the environment variable ANT_HOME to C:Program FilesApache Software Foundationapache-ant-1.8.4

2

Linux

export ANT_HOME = /usr/local/apache-ant-1.8.4

3

Mac

export ANT_HOME = /Library/apache-ant-1.8.4

Append Ant compiler location to the System Path as follows −

OS Output
Windows Append the string %ANT_HOMEin at the end of the system variable, Path.
Linux export PATH = $PATH:$ANT_HOME/bin/
Mac not required

Step 3: Download JUnit Archive

Download a JUnit Archive that suits your operating system.

OS Archive Name
Windows junit4.10.jar
Linux junit4.10.jar
Mac junit4.10.jar

Step 4: Create Project Structure

    Create a folder TestJunitWithAnt in C:>JUNIT_WORKSPACE.

    Create a folder src in C:>JUNIT_WORKSPACE>TestJunitWithAnt.

    Create a folder test in C:>JUNIT_WORKSPACE>TestJunitWithAnt.

    Create a folder pb in C:>JUNIT_WORKSPACE>TestJunitWithAnt.

    Create MessageUtil class in C:>JUNIT_WORKSPACE>TestJunitWithAnt> srcfolder.

/*
* This class prints the given message on console.
*/

pubpc class MessageUtil {

   private String message;

   //Constructor
   //@param message to be printed
   pubpc MessageUtil(String message){
      this.message = message; 
   }

   // prints the message
   pubpc String printMessage(){
      System.out.println(message);
      return message;
   }   

   // add "Hi!" to the message
   pubpc String salutationMessage(){
      message = "Hi!" + message;
      System.out.println(message);
      return message;
   }   
}  	

Create TestMessageUtil class in the folder C:>JUNIT_WORKSPACE>TestJunitWithAnt>src.

import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;

pubpc class TestMessageUtil {

   String message = "Robert";	
   MessageUtil messageUtil = new MessageUtil(message);
   
   @Test
   pubpc void testPrintMessage() {	
      System.out.println("Inside testPrintMessage()");     
      assertEquals(message,messageUtil.printMessage());
   }

   @Test
   pubpc void testSalutationMessage() {
      System.out.println("Inside testSalutationMessage()");
      message = "Hi!" + "Robert";
      assertEquals(message,messageUtil.salutationMessage());
   }
}

Copy junit-4.10.jar onto the folder C:>JUNIT_WORKSPACE>TestJunitWithAnt>pb.

Create ANT Build.xml

We ll be using <junit> task in Ant to execute our JUnit test cases.

<project name = "JunitTest" default = "test" basedir = ".">
   <property name = "testdir" location = "test" />
   <property name = "srcdir" location = "src" />
   <property name = "full-compile" value = "true" />
	
   <path id = "classpath.base"/>
	
   <path id = "classpath.test">
      <pathelement location = "pb/junit-4.10.jar" />
      <pathelement location = "${testdir}" />
      <pathelement location = "${srcdir}" />
      <path refid = "classpath.base" />
   </path>
	
   <target name = "clean" >
      <delete verbose = "${full-compile}">
         <fileset dir = "${testdir}" includes = "**/*.class" />
      </delete>
   </target>
	
   <target name = "compile" depends = "clean">
      <javac srcdir = "${srcdir}" destdir = "${testdir}" 
         verbose = "${full-compile}">
         <classpath refid = "classpath.test"/>
      </javac>
   </target>
	
   <target name = "test" depends = "compile">
      <junit>
         <classpath refid = "classpath.test" />
         <formatter type = "brief" usefile = "false" />
         <test name = "TestMessageUtil" />
      </junit>
   </target>
	
</project>

Run the following Ant command.

C:JUNIT_WORKSPACETestJunitWithAnt>ant

Verify the output.

Buildfile: C:JUNIT_WORKSPACETestJunitWithAntuild.xml

clean:  

compile:  
   [javac] Compipng 2 source files to C:JUNIT_WORKSPACETestJunitWithAnt	est
   [javac] [parsing started C:JUNIT_WORKSPACETestJunitWithAntsrc
      MessageUtil.java]
   [javac] [parsing completed 18ms]
   [javac] [parsing started C:JUNIT_WORKSPACETestJunitWithAntsrc
      TestMessageUtil.java]
   [javac] [parsing completed 2ms]
   [javac] [search path for source files: C:JUNIT_WORKSPACE
      TestJunitWithAntsrc]    
   [javac] [loading javalangObject.class(javalang:Object.class)]
   [javac] [loading javalangString.class(javalang:String.class)]
   [javac] [loading orgjunitTest.class(orgjunit:Test.class)]
   [javac] [loading orgjunitIgnore.class(orgjunit:Ignore.class)]
   [javac] [loading orgjunitAssert.class(orgjunit:Assert.class)]
   [javac] [loading javalangannotationRetention.class
      (javalangannotation:Retention.class)]
   [javac] [loading javalangannotationRetentionPopcy.class
      (javalangannotation:RetentionPopcy.class)]
   [javac] [loading javalangannotationTarget.class
      (javalangannotation:Target.class)]
   [javac] [loading javalangannotationElementType.class
      (javalangannotation:ElementType.class)]
   [javac] [loading javalangannotationAnnotation.class
      (javalangannotation:Annotation.class)]
   [javac] [checking MessageUtil]
   [javac] [loading javalangSystem.class(javalang:System.class)]
   [javac] [loading javaioPrintStream.class(javaio:PrintStream.class)]
   [javac] [loading javaioFilterOutputStream.class
      (javaio:FilterOutputStream.class)]
   [javac] [loading javaioOutputStream.class(javaio:OutputStream.class)]
   [javac] [loading javalangStringBuilder.class
      (javalang:StringBuilder.class)]
   [javac] [loading javalangAbstractStringBuilder.class
      (javalang:AbstractStringBuilder.class)]
   [javac] [loading javalangCharSequence.class(javalang:CharSequence.class)]
   [javac] [loading javaioSeriapzable.class(javaio:Seriapzable.class)]
   [javac] [loading javalangComparable.class(javalang:Comparable.class)]
   [javac] [loading javalangStringBuffer.class(javalang:StringBuffer.class)]
   [javac] [wrote C:JUNIT_WORKSPACETestJunitWithAnt	estMessageUtil.class]
   [javac] [checking TestMessageUtil]
   [javac] [wrote C:JUNIT_WORKSPACETestJunitWithAnt	estTestMessageUtil.class]
   [javac] [total 281ms]

test:
    [junit] Testsuite: TestMessageUtil
    [junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.008 sec
    [junit]
    [junit] ------------- Standard Output ---------------
    [junit] Inside testPrintMessage()
    [junit] Robert
    [junit] Inside testSalutationMessage()
    [junit] Hi!Robert
    [junit] ------------- ---------------- ---------------

BUILD SUCCESSFUL
Total time: 0 seconds
Advertisements