1
votes

I am using JDK 1.7, eclipse 4.2.2, JUnit 4.8.1, ant 1.9.2, windows8 64 bit** When I am running my build.xml, all targets run fine but the target with name as run is not running fine.

build.xml

<?xml version="1.0" encoding="UTF-8"?>
  <project name="AntExample" default="setclasspath" basedir=".">
    <!-- Properties Initialization -->
    <property environment="env"/> 
    <property name="ws.home" value="${basedir}" />
    <property name="ws.jar" value="${basedir}/lib" />
    <property name="test.dest" value="${basedir}/build" />
    <property name="test.src" value="${basedir}/src" />
    <property name="test.reportsDir" value="E:\Reports" />

    <!-- Path Initialization -->
    <path id="testcase.path">
      <pathelement location="${ws.jar}"/>
      <pathelement path="${classes}"/>
      <fileset dir="${ws.jar}">
        <include name="**/*.jar"/>
      </fileset>
    </path>

    <!-- Target Setclasspath -->
    <target name="setclasspath" unless="testcase.path">
      <path id="classpath_jars">
        <fileset dir="${ws.jar}">
          <include name="**/*.jar"/>
        </fileset>
      </path>
      <pathconvert pathsep=";" property="testcase.path" refid="classpath_jars"/>
    </target>

    <!-- Target init -->
    <target name="init" depends="setclasspath">
      <condition property="ANT"
                 value="${env.ANT_HOME}/bin/ant.bat"
                 else="${env.ANT_HOME}/bin/ant">
        <os family="windows"/>
      </condition>
    </target>

    <!-- Target clean -->
    <target name="clean">
      <delete dir="${test.dest}"/>
    </target>

    <!-- Target compile -->
    <target name="compile" depends="init,clean">
      <delete includeemptydirs="true" quiet="true">
        <fileset dir="${test.dest}">
          <include name="**/*"/>
        </fileset>
      </delete>
      <mkdir dir="${test.dest}"/>
      <javac debug="true"
             destdir="${test.dest}"
             srcdir="${test.src}"
             target="1.7"
             classpath="${testcase.path}"/>
    </target>

    <!-- Target run -->
    <target name="run" >
      <delete includeemptydirs="true" quiet="true">
        <fileset dir="${test.reportsDir}">
          <include name="**/*"/>
        </fileset>
      </delete> 
      <mkdir dir="${test.reportsDir}"/>
      <java jar="${ws.jar}"  fork="yes" spawn="yes"/>
      <junit fork="yes" haltonfailure="no" printsummary="yes" >
        <classpath refid="testcase.path" />
        <batchtest todir="${test.reportsDir}" fork="true">
          <fileset dir="${test.dest}">
            <include name="anttestcase/Login.class"/>
          </fileset>
        </batchtest>
        <formatter type="xml"/>
      </junit>

      <junitreport todir="${test.reportsDir}">
        <fileset dir="${test.reportsDir}">
          <include name="TESTS-*.xml"/>
        </fileset>
        <report todir="${test.reportsDir}"/>
      </junitreport>
    </target>
  </project>`

When I am running my build with command prompt, i am getting the below error:

C:\Users\Ashish Rathore\workspace\AntExample>ant run

Buildfile: C:\Users\Ashish Rathore\workspace\AntExample\build.xml

run: [junit] WARNING: multiple versions of ant detected in path for junit [junit]
jar:file:/H:/ant1.9/apache-ant-1.9.2-bin/apache-ant-1.9.2/lib/ant.jar!/org/apache/tools/ant/Project.class [junit]and jar:file:/C:/Users/Ashish%20Rathore/workspace/AntExample/lib/ant.jar!/org/apache/tools/ant/Project.class [junit]Running anttestcase.Login [junit]Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec [junit] Test anttestcase.Login FAILED [junitreport] Processing E:\Reports\TESTS-TestSuites.xml to C:\Users\ASHISH~1\Ap pData\Local\Temp\null1158960870 [junitreport] Loading stylesheet jar:file:/H:/ant1.9/apache-ant-1.9.2-bin/apache -ant-1.9.2/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/j unit-frames.xsl [junitreport] Transform time: 1223ms [junitreport] Deleting: C:\Users\ASHISH~1\AppData\Local\Temp\null1158960870

BUILD SUCCESSFUL Total time: 5 seconds

Error message in TEST-anttestcase.Login.xml in my Reports

error type="java.lang.ClassNotFoundException" message="anttestcase.Login">
 java.lang.ClassNotFoundException: anttestcase.Login at
   java.net.URLClassLoader$1.run(URLClassLoader.java:366) at
   java.net.URLClassLoader$1.run(URLClassLoader.java:355) at
   java.security.AccessController.doPrivileged(Native Method) at
   java.net.URLClassLoader.findClass(URLClassLoader.java:354) at
   java.lang.ClassLoader.loadClass(ClassLoader.java:423) at
   sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at
   java.lang.ClassLoader.loadClass(ClassLoader.java:356) at
   java.lang.Class.forName0(Native Method) at
   java.lang.Class.forName(Class.java:188)

My Login.java test case

package anttestcase;

import junit.framework.Assert;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;

public class Login {
  @Rule public ErrorCollector errCollector=new ErrorCollector();

  @BeforeClass public static void setUp() throws Exception {
    System.out.println("opening Url");
  }

  @AfterClass public static void tearDown() throws Exception {
  }

  @Test public void enterCredentials() {
    try {
      Assert.assertEquals("A", "B");
      System.out.println("Enter Username and Password");
    } catch(Throwable t) {
      errCollector.addError(t);   
      System.out.println("Caught");
    }
  }

  @Test public void authenticityCheck() {
    System.out.println("Login Successfully");
  }
}
1

1 Answers

5
votes

You must add the compiled classes to the class path of the junit task.

    <junit fork="yes" haltonfailure="no" printsummary="yes" >
    <classpath>
      <path refid="testcase.path">
      <pathelement location="${test.dest}"/>
    </classpath>
    <batchtest todir="${test.reportsDir}" fork="true">
      <fileset dir="${test.dest}">
        <include name="anttestcase/Login.class"/>
      </fileset>
    </batchtest>
    <formatter type="xml"/>
  </junit>

On Github there's an example ant project, which uses JUnit: https://github.com/mplacona/java-junit-template-project Have a look at it's build.xml.