我是Java新手,所以请对我温柔一点。我正在尝试蚂蚁构建系统运行我的JUnit测试。但是当我在没有Ant的情况下运行测试时,它通过了,但是当我使用Ant Build来运行JUnit测试时,测试失败了。
这是我的Ant build.xml
文件:
<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>
和Ant Build的堆栈跟踪是:
/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
测试文件为:
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());
}
}
主文件是:
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;
}
}
目录结构为:
├── 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个目录,12个文件有人能帮我一下吗?
我设法让它与下面的构建文件一起工作。Ant试图将其作为junit 3风格测试而不是junit 4来运行。我相信这是因为语言级别默认为1.5。我还必须编译测试。之后,org/hamcrest/SelfDescribing
出现了NoClassDef错误。通过将hamcrest jar添加到lib中可以解决这个问题。
如何修复:
- 设置1.5以上的源版本 编译测试类
- 添加hamcrest-all-1.3.jar到
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>