6
我是Java新手,所以請對我溫柔。我正在嘗試Ant構建系統來運行我的JUnit測試。但是當我在沒有Ant的情況下運行我的測試時,它會通過,但是當我使用Ant Build來運行我的JUnit測試時,測試失敗。爲什麼JUnit測試在Ant Build System中失敗?
這裏是我的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構建的堆棧跟蹤是:
/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個文件 誰能幫我這個? 在此先感謝。
http://stackoverflow.com/questions/7850847/junit-test-fails-in-ant-with-junit-task-but-passes-with-exec的可能的複製 – Codebender
我認爲那是不同的,試圖這樣,但它不起作用。這是因爲我只通過JUnit從命令行以及IDEA IDE嘗試它,它工作。但是當我使用shell或IDE來運行Ant腳本時,這兩種方式都失敗了。 –
Codebender是正確的。即使這不是一個可能的重複,原因也與丟失的JAR或類路徑問題有關。 – Geek