Olaf458
Olaf458

Reputation: 23

Using junit in ant

I am trying to run my tests using junit, compile returns success, but when I run it, i get this error:

Error: Could not find or load main class Homewok2Test.class

Here is my build.xml:

<project name="othello" basedir=".">
    <target name="compile">
      <javac srcdir="src" destdir="build">
         <classpath>
            <pathelement path="lib/junit-4.12.jar"/>
            <pathelement path="lib/hamcrest-core-1.3.jar"/> 
         </classpath>
       </javac>
       <jar jarfile="dest-client/othello-client.jar"   basedir="build">
        <manifest>
            <attribute name="Main-Class" value="Homewok2Test.class" />
        </manifest>
       </jar>
     </target>
    <target name="run">   
        <java jar="dest-client/othello-client.jar" fork="true"/>
    </target>
</project>

Upvotes: 1

Views: 116

Answers (2)

Olaf458
Olaf458

Reputation: 23

So I added

 <target name="run">   
            <junit printsummary="yes" haltonfailure="yes">
                <classpath>
                    <pathelement path="lib/junit-4.12.jar"/>
                    <pathelement path="lib/hamcrest-core-1.3.jar"/>
                    <pathelement path="dest-client/othello-client.jar"/>
                    <pathelement location="lib/junit-4.12.jar"/>
                    <pathelement location="lib/hamcrest-core-1.3.jar"/>
                    <pathelement location="dest-client/othello-client.jar"/>
                </classpath>
                <test name="Homework2Test" haltonfailure="yes" outfile="result">
                    <formatter type="plain"/>
                    <formatter type="xml"/>
                </test>
            </junit>
<target/>

Next problem was spelling, in my first build I have Homewok2Test, which is incorrect. Correct class was Homework2Test

Upvotes: 0

ManoDestra
ManoDestra

Reputation: 6473

Amend this...

<attribute name="Main-Class" value="Homewok2Test.class" />

To...

<attribute name="Main-Class" value="Homework2Test" />

NB your value above has a typo, which could be another issue for you: Homewok.

And ensure that your Homework2Test class has a main method something like this...

public static void main(String[] args) {
    org.junit.runner.JUnitCore runner = new org.junit.runner.JUnitCore();
    org.junit.runner.Result result = runner.run(Homework2Test.class);
}

The value is the fully qualified package path to your class, not the physical class file path. Then when you run the jar, it will automatically pick up the Main-Class attribute and run that class accordingly.

If you're running your JUnit tests from your Homework2Test class, then everything should be fine, but if you're not, then you may wish to run JUnit from Ant.

Something like this...

<target name="unit-test-1" >
  <junit printsummary="yes" haltonfailure="yes">
    <classpath>
      <pathelement location="${project.class.path}"/>
      <pathelement location="${build.tests}"/>
      <pathelement path="${java.class.path}"/>
    </classpath>
    <test name="com.jenkov.test.MyUnitTest"
             haltonfailure="no" outfile="result">
      <formatter type="plain"/>
      <formatter type="xml"/>
    </test>
  </junit>
</target>

Taken from this tutorial

Or this tutorial may also be of use.

Upvotes: 1

Related Questions