为什么JUnit测试在Ant Build System中失败?

时间:2015-07-20 12:52:40

标签: java ant junit

我是Java的新手,所以请温柔地对待我。我正在尝试使用Ant Build System来运行我的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个文件 谁可以帮我这个事? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我设法让它使用下面的构建文件。 Ant试图将它作为junit 3样式测试而不是4来运行。我相信这是因为语言级别默认为1.5。我还必须编译测试。之后,org/hamcrest/SelfDescribing出现了NoClassDef错误。这是通过将hamcrest jar添加到lib来解决的。

如何解决:

  1. 将源版本设置为高于1.5
  2. 编译测试类
  3. hamcrest-all-1.3.jar添加到lib
  4. <强>的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>