0
votes

I am new to Ant and getting java.lang.ClassNotFoundException. when running my Junit from eclipse / Ant build.xml, However, when running my unit test from eclipse by itself my test passes with no issue. There should be something wrong with my classpath which i cannot figure out.

My envrmnts are:

Java_home: C:/Program Files/Java/jdk1.7.0_25 Ant_home: C:/Users/Armen/javaFolder/apache-ant-1.9.2/bin JUnit_home: C:/Users/Armen/javaFolder/apache-ant-1.9.2/bin/junit-4.10.jar

My Build.xml

<property name="junitLocation">C:/Users/Armen/javaFolder/apache-ant-1.9.2/bin/junit-4.10.jar</property>
<property name="antLocation2">C:/Users/Armen/JavaFolder/apache-ant-1.9.2.jar</property>
<property name="testCode">C:/Users/Armen/workspace/MockingObjects/test/demo</property>
<property name="srcCode">C:/Users/Armen/workspace/MockingObjects/src/demo</property>

<target name="compile" depends="clean">
        <javac includeantruntime="false" srcdir="./src" destdir="./staging" ></javac>
</target>

<target name="run" depends="compile, unitTest">
    <java classname="demo.AccountService"><classpath path="./staging"></classpath></java>
</target>

<target name="unitTest" depends="compile">
    <junit printsummary="true" showoutput="true" haltonfailure="false" fork="yes">

        <formatter type="plain" usefile="true"/>
        <test name="demo.TestAccountService" outfile="./result" ></test>

        <classpath> 
            <pathelement location="${junitLocation}"/>
            <pathelement location="${antLocation}"/>
            <pathelement location="${testCode}" />
            <pathelement location="${srcCode}"/>
        </classpath>

    </junit>
</target>

<target name="clean">
    <delete dir="staging"></delete>
    <mkdir dir="./staging"/>
</target>

enter image description here

1
Which ClassNotFoundException? check the class and find out the jar file for the same in your class path?CHowdappaM

1 Answers

0
votes

You don't have any step in your build script to compile the unit tests, and when you tell JUnit to run, you also don't include the compiled classes in the classpath - only the source files.

There are therefore 2 things you need to do:

  1. Add an additional build step similar to the following

    <target name="test-compile" depends="compile">
        <javac includeantruntime="false" srcdir="./test" destdir="./test-classes" />
    </target>
    

then change your current unitTest target to depend on test-compile rather than compile.

  1. Tell JUnit where your classes are, not your source code. Change your testCode and srcCode properties to

    <property name="testCode">C:/Users/Armen/workspace/MockingObjects/test-classes</property>
    <property name="srcCode">C:/Users/Armen/workspace/MockingObjects/staging</property>
    

Note From your compile and run steps, it isn't clear if your code is properly structured in Java Packages or, if it is in packages, that you understand how these work. I've made the assumption that your code is in a package of demo and therefore stripped that part of the path out of your compiled class locations.