2015-07-20 37 views
6

Jestem nowy w Javie, więc proszę, bądź dla mnie łagodny. Próbuję Ant Build System, aby uruchomić moje testy JUnit. Ale kiedy uruchamiam moje testy bez Anta, to mija, ale kiedy używam Ant Build do uruchamiania moich testów JUnit, testy się nie udają.Dlaczego testy JUnit nie działają w systemie budowy mrówek?

Oto moja 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> 

A ślady stos Ant produkcji jest:

/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 

Plik test jest:

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()); 
    } 
} 

A głównym pliku jest:

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; 
    } 
} 

Struktura katalogu jest:

├── 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 katalogów plików Czy ktoś może mi pomóc w tej sprawie? Z góry dzięki.

+2

Możliwe duplikaty http://stackoverflow.com/questions/7850847/junit-test-fails-in-ant-with-junit-task-but-passes-w-exec – Codebender

+0

Myślę, że to jest inne, wypróbowane w ten sposób, ale to nie działa. To dlatego, że próbowałem go tylko JUnit z linii poleceń, a także IDEA IDE, działa. Ale kiedy używam powłoki lub IDE do uruchamiania Ant Script, to nie działa na oba sposoby. –

+2

Codebender ma rację. Nawet jeśli nie jest to możliwy duplikat, ma to związek z brakującym problemem JAR lub classpath. – Geek

Odpowiedz

-1

Udało mi się uruchomić go z plikiem kompilacji poniżej. Ant próbował uruchomić to jako 3-stykowy test zamiast 4. Uważam, że było to spowodowane poziomem językowym wynoszącym 1.5. Musiałem również skompilować testy. Po tym wystąpił błąd NoClassDef dla org/hamcrest/SelfDescribing. Zostało to rozwiązane przez dodanie słoja hamcrest do lib.

Jak naprawić:

  1. Ustaw wersję źródło powyżej 1,5
  2. skompilować klas testowych
  3. Dodaj hamcrest-all-1.3.jar do 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>