I am new to Java, So please be gentle on me. I am trying Ant Build System to run my JUnit Tests. But When I am running my tests without Ant, It passes, but when I am using Ant Build to run my JUnit tests, the tests are failing.
Here is my Ant build.xml
file:
<property name="lib.dir" value="lib"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="*.jar"/>
</path>
<target name="runjunit" depends="compile">
<mkdir dir="reports" />
<junit printsummary="true" showoutput="yes" haltonerror="yes" haltonfailure="yes" >
<test name="com.geometry.CartesianPositionTest" />
<classpath>
<path refid="classpath" />
<pathelement location="out" />
</classpath>
</junit>
</target>
<target name="compile">
<javac srcdir="src" destdir="out" includeantruntime="false" classpath="lib/junit-4.11.jar"/>
</target>
<target name="package" depends="compile">
<jar destfile="geometry.jar"
basedir="out"
includes="**"
excludes="**/*Test*" >
<manifest>
<attribute name="Main-Class" value="com.geometry.CartesianPosition" />
</manifest>
</jar>
</target>
And the stack traces of Ant Build is:
/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java -Xmx128m -Xss2m "-Dant.home=/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant" "-Dant.library.dir=/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib" -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/javafx-doclet.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/tools.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-antlr.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-apache-bcel.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-apache-bsf.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-apache-log4j.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-apache-oro.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-apache-regexp.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-apache-resolver.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-apache-xalan2.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-commons-logging.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-commons-net.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-jai.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-javamail.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-jdepend.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-jmf.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-jsch.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-junit.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-junit4.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-launcher.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-netrexx.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-swing.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant-testutil.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/ant/lib/ant.jar:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/idea_rt.jar" com.intellij.rt.ant.execution.AntMain2 -logger com.intellij.rt.ant.execution.IdeaAntLogger2 -inputhandler com.intellij.rt.ant.execution.IdeaInputHandler -buildfile /Users/rajatg/geometry/build.xml runjunit
build.xml
property
path
compile
javac
runjunit
mkdir
junit
Running com.geometry.CartesianPositionTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
/Users/rajatg/geometry/build.xml:13: Test com.geometry.CartesianPositionTest failed
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.actOnTestResult(JUnitTask.java:2150)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:1025)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:2095)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:828)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
at org.apache.tools.ant.Main.runBuild(Main.java:851)
at org.apache.tools.ant.Main.startAnt(Main.java:235)
at org.apache.tools.ant.Main.start(Main.java:198)
at org.apache.tools.ant.Main.main(Main.java:286)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.ant.execution.AntMain2.main(AntMain2.java:30)
/Users/rajatg/geometry/build.xml:13: Test com.geometry.CartesianPositionTest failed
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.actOnTestResult(JUnitTask.java:2150)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:1025)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:2095)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:828)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
at org.apache.tools.ant.Main.runBuild(Main.java:851)
at org.apache.tools.ant.Main.startAnt(Main.java:235)
at org.apache.tools.ant.Main.start(Main.java:198)
at org.apache.tools.ant.Main.main(Main.java:286)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.ant.execution.AntMain2.main(AntMain2.java:30)
/Users/rajatg/geometry/build.xml:13: Test com.geometry.CartesianPositionTest failed
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.actOnTestResult(JUnitTask.java:2150)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:1025)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:2095)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:828)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
at org.apache.tools.ant.Main.runBuild(Main.java:851)
at org.apache.tools.ant.Main.startAnt(Main.java:235)
at org.apache.tools.ant.Main.start(Main.java:198)
at org.apache.tools.ant.Main.main(Main.java:286)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.ant.execution.AntMain2.main(AntMain2.java:30)
Ant build completed with 3 errors and no warnings in 0s at 20/07/15 6:15 PM
The test file is:
package com.geometry;
import org.junit.Test;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
public class CartesianPositionTest {
@Test
public void testShouldReturnTheEndPointsOfLine() throws Exception {
CartesianPosition position = new CartesianPosition(0, 0, 0, 0);
assertEquals(Arrays.asList(0, 0, 1, 1), position.getPositions());
}
@Test
public void testCheckForLengthBetweenZeroAndOne() throws Exception {
CartesianPosition line = new CartesianPosition(0, 0, 1, 1);
assertEquals(1, line.length());
}
}
And the main file is:
package com.geometry;
import java.util.ArrayList;
public class CartesianPosition {
private int x1, y1, x2, y2;
public int length;
public CartesianPosition(int positionX1, int positionY1, int positionX2, int positionY2) {
this.x1 = positionX1;
this.y1 = positionY1;
this.x2 = positionX2;
this.y2 = positionY2;
}
public Object getPositions() {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add (0);
list.add (0);
list.add (1);
list.add (1);
return list;
}
public int length() {
return 1;
}
}
The directory structure is:
├── README.md
├── TEST-com.geometry.CartesianPositionTest.xml
├── build.xml
├── geometry.iml
├── geometry.jar
├── lib
│ ├── hamcrest-core-1.3.jar
│ └── junit-4.11.jar
├── out
│ ├── com
│ │ └── geometry
│ │ └── CartesianPosition.class
│ ├── production
│ │ └── geometry
│ │ └── com
│ │ └── geometry
│ │ └── CartesianPosition.class
│ └── test
│ └── geometry
│ └── com
│ └── geometry
│ └── CartesianPositionTest.class
├── reports
├── src
│ └── com
│ └── geometry
│ └── CartesianPosition.java
└── test
└── com
└── geometry
└── CartesianPositionTest.java
19 directories, 12 files Can anyone help me on this? Thanks in advance.
I managed to get it working with the build file below. Ant was attempting to run it as a junit 3 style test instead of 4. I believe this was because of the language level defaulting 1.5. I also had to compile the tests. After that there was an NoClassDef error for org/hamcrest/SelfDescribing
. This was solved by adding the hamcrest jar to lib.
How to fix:
lib
build.xml
<project name="CartesianPosition" basedir=".">
<property name="java.sdk" value="1.6"/>
<property name="lib.dir" value="lib"/>
<property name="main.src" value="src"/>
<property name="main.out" value="out/classes"/>
<property name="test.src" value="test"/>
<property name="test.out" value="out/testClasses"/>
<property name="reports" value="reports"/>
<property name="artifact" value="out/geometry.jar"/>
<path id="mainClasspath">
<path>
<fileset dir="${lib.dir}" includes="*.jar"/>
</path>
</path>
<path id="testClasspath">
<path refid="mainClasspath"/>
<pathelement location="${main.out}"/>
<pathelement location="${test.out}"/>
</path>
<target name="clean">
<delete dir="out"/>
<delete dir="${reports}"/>
</target>
<target name="compile">
<mkdir dir="${main.out}"/>
<javac srcdir="${main.src}"
destdir="${main.out}"
includeantruntime="false"
classpathref="mainClasspath"
target="${java.sdk}" source="${java.sdk}"/>
</target>
<target name="testCompile" depends="compile">
<mkdir dir="${test.out}"/>
<javac srcdir="${test.src}"
destdir="${test.out}"
includeantruntime="false"
classpathref="testClasspath"
target="${java.sdk}" source="${java.sdk}"/>
</target>
<target name="test" depends="testCompile">
<mkdir dir="${reports}"/>
<junit printsummary="withOutAndErr">
<classpath refid="testClasspath"/>
<formatter type="xml"/>
<batchtest fork="true" todir="${reports}">
<fileset dir="${test.out}">
<include name="**/*Test.class"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="package" depends="compile">
<jar destfile="${artifact}" includes="${main.out}">
<manifest>
<attribute name="Main-Class" value="com.geometry.CartesianPosition"/>
</manifest>
</jar>
</target>
</project>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With