2011-12-10 37 views
1

我從eclipse和Ant任務運行相同的測試。從eclipse運行時,所有測試都會通過。當我運行Ant junit任務時,單個測試失敗,出現以下奇怪錯誤:僅當從Ant任務運行時,單元測試纔會失敗

junit.framework.AssertionFailedError at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32) at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:423) at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:137) at unitests.mypackage.MyTestClass.myTestCase(Unknown Source)

原因是什麼?

我讀了一點,發現它可能是因爲eclipse和Ant使用不同版本的junit。在我的項目中,junit位於libs/junit-4.10.jar中,並在eclipse的.classpath文件和junit任務類路徑中引用。 你可以看到Ant的任務在這裏:

<path id="classpath"> 
    <fileset dir="${lib.dir}" includes="**/*.jar"/> 
    <fileset dir="${src.dir}" includes="**/*.jar"/> 
</path> 
... 
<target name="run-unit-tests" depends="compile,compile-unit-tests"> 
    <mkdir dir="${junit.output.dir}"/> 
    <junit fork="yes" printsummary="yes" haltonfailure="no">   
     <classpath> 
      <path refid="classpath"/> 
      <fileset dir="${unit.tests.classes.dir}" includes="**/*.class"/> 
     </classpath> 

     <formatter type="xml"/> 
     <batchtest todir="${junit.output.dir}"> 
      <fileset dir="${unit.tests.dir}"> 
       <include name="**/*Test*.java"/> 
      </fileset> 
     </batchtest> 
    </junit> 

    <mkdir dir="${junit.report.dir}"/> 
    <junitreport todir="${junit.report.dir}"> 
     <fileset dir="${junit.output.dir}"> 
     <include name="TEST-*.xml"/> 
     </fileset> 
     <report format="frames" todir="${junit.report.dir}/html"/> 
    </junitreport> 
</target> 

螞蟻的版本是1.7.1,並與日食來了。

編輯:

加入fork="yes「JUnit的任務,最終解決它通過使用Eclipse的導出選項生成構建文件,然後看着生成的文件和我之間的差異發現它不知道爲什麼。分叉雖然解決了這個問題。

回答

2

由於堆棧跟蹤顯示org.eclipse條目,我強烈懷疑您通過Eclipse運行ant。如果你從控制檯運行ant,你很可能不會遇到這個問題。

因此,原因是Eclipse中的一個錯誤。這應該不足爲奇,Eclipse充滿了bug。

fork="true"有幫助,因爲它會導致螞蟻產生一個新的執行過程。這個新進程在類路徑中沒有任何Eclipse類。

3

不同版本的螞蟻聽起來像一個可能的原因。爲了驗證這一點,你要Eclipse中使用set which Ant version

這可能也設置相同的版本是個好主意JUnit和用於在IDE外部運行測試的same JDK

+0

我不明白。你是否建議安裝獨立的Ant並使用它? – Artium

+0

我假設你已經從命令行運行你的構建,並在類路徑中引用該Ant jar(不知道你的意思是獨立的Ant)。既然你確定JUnit版本是相同的,我會檢查Eclipse是否使用Ant的相同版本作爲類路徑引用。上面的鏈接應該指向Eclipse的ant首選項。發現了一篇引用類似錯誤的帖子,聲稱問題出在JUnit版本上,所以可能值得雙重檢查,兩者都使用相同的JUnit:http://www.coderanch.com/t/96244/Testing/junit-exception –

相關問題