0
votes

Im trying to run some JUnit tests using a Ant script. I can run them in Eclipse, but I am having problems when doing it using Ant.

I have a simple class, called Calculator that does some calculations, this is my class:

Calculator.java

package CalculatorSrc;

public class Calculator {

    public double add(double n1, double n2){
        return n1+n2;
    }   

    //...

}

And then I have another class to test my methods like:

TestCalculator.java

package CalculatorTests;

import static org.junit.Assert.*;

import org.junit.*;

import CalculatorSrc.Calculator;

public class TestCalculator {

    @Test
    public void testAdd(){
        Calculator calc = new Calculator();
        double result = calc.add(10, 50);
        assertEquals(60, result, 0);
    }   

    //...

}

My project dir is this:

project dir

Finally, my Ant script to run JUnit tests is this:

<property name="build.classes.dir" location="build/classes"/>

<property name="class.src.dir" location="src/CalculatorSrc"/>
<property name="tests.src.dir" location="src/CalculatorTests"/>
<property name="reports.dir" location="reports"/>

<path id="classpath.base" />

<path id="test.classpath">
    <pathelement location="${class.src.dir}"/>
    <pathelement location="${tests.src.dir}"/>
    <pathelement location="${build.classes.dir}"/>
    <pathelement location="tools/JUnit/junit-4.12.jar"/>
    <pathelement location="tools/JUnit/hamcrest-core-1.3.jar"/>
    <pathelement location="tools/JUnit/ant-junit-1.9.4.jar"/>
</path>

<target name="build">
    <antcall target="test-compile"/>
    <antcall target="test"/>
</target>

<target name="test-compile">
    <javac includeantruntime="false" srcdir="${class.src.dir}" destdir="${build.classes.dir}" debug="true">
        <classpath refid="classpath.base"/>
    </javac>
    <javac includeantruntime="false" srcdir="${tests.src.dir}" destdir="${build.classes.dir}" debug="true">
        <classpath refid="test.classpath"/>
    </javac>
</target>

<target name="test">
    <junit haltonfailure="no" failureproperty="failed">
        <classpath>
            <path refid="test.classpath"/>
            <pathelement location="${build.classes.dir}"/>
        </classpath>
        <formatter type="xml"/>
        <batchtest fork="yes" todir="${reports.dir}">
            <fileset dir="${tests.src.dir}" includes="**/*Test*.java"/>
        </batchtest>
    </junit>
    <fail message="TEST FAILURE" if="failed"/>
</target>

When I run the Ant script using Jenkis my build fails... If I go check the xml file created I see the error ClassNotFoundException...

enter image description here

What am I doing wrong?

EDIT:

Here are some prints of the output of Jenkins when I build my project:

As you can see in the next screenshot jenkins can find my build.xml...

jenkins build

And here is the error that I have:

enter image description here

The line 57 of my build.xml is <antcall target="test"/> and the line 121 is <fail message="TEST FAILURE" if="failed"/>.

1
How do you start ant?Stefan Birkner
I execute it using jenkins.lulas
Which command is used by Jenkins?Stefan Birkner
I dont know, I use the interface of jenkins to run Ant. In jenkis the only thing I do is to create a project, give the build.xml location and then start the build, I added some prints of the output when I build my project.lulas

1 Answers

1
votes

You are using directories and packages wrong. src/CalculatorSrc and src/CalculatorTests are both source folders, but in Eclipse you're using the parent directory src as source folder. What to do:

  • Change the project setting in Eclipse: src/CalculatorSrc and src/CalculatorTests are both source folders
  • Remove the package declaration from Calculator.java and CalculatorTest.java (the first line of each file)
  • Remove the line import CalculatorSrc.Calculator; from CalculatorTest.java