2017-04-18 65 views
0

測試(存在於項目tests.jar):螞蟻+ TestNG的:異常在線程 「主要」 java.lang.NoClassDefFoundError:COM /博伊斯特/ jcommander/ParameterException

package sample; 

import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 
import org.testng.annotations.Test; 
import utils.Base; 

public class SampleTest { 
    @Test 
    public void Test() { 
     log.info("Sample Test called successfully."); 

     log.info("Invoking Base Method"); 
     Base b = new Base(); 
     b.testMethod(); 
    } 
} 

的Util(存在於項目。罐):

package utils; 

import org.apache.commons.configuration.HierarchicalConfiguration; 
import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 

import java.util.Map.Entry; 

public class Base { 
    public void testMethod() { 
     log.info("Test method in utils.Base class called."); 
    } 
} 

假設日誌已經在兩個類中創建和使用。

在build.xml:

<project default="TestNGSimpleTest"> 

    <path id="main_cp"> 
     <pathelement location="dependency/testng-6.9.10.jar"/> 
     <pathelement location="dependency"/> 
     <pathelement location="."/> 
    </path> 

    <taskdef name="testng" classpathref="main_cp" 
      classname="org.testng.TestNGAntTask" /> 

    <target name="test"> 
     <testng classpathref="main_cp" > 
      <classfileset dir="." includes="sample/*.class"/> 
     </testng> 
    </target> 

</project> 

目標目錄結構:

./dependency/{all-dependencies}.jar 
./project.jar 
./project-tests.jar 
./build.xml 

{全依賴}包括:

bsh-1.3.0.jar 
bsh-2.0b4.jar 
commons-beanutils-1.7.0.jar 
commons-beanutils-core-1.8.0.jar 
commons-collections-3.2.1.jar 
commons-configuration-1.6.jar 
commons-digester-1.8.jar 
commons-lang-2.4.jar 
commons-logging-1.1.1.jar 
jcommander-1.48.jar 
testng-6.9.10.jar 

當我從IDE運行SampleTest它運行良好,但是當我嘗試使用ant(ant -v test)運行它時,出現以下錯誤:

[testng] The ' characters around the executable and arguments are 
[testng] not part of the command. 
[testng] Error: A JNI error has occurred, please check your installation and try again 
[testng] Exception in thread "main" java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException 
[testng] at java.lang.Class.getDeclaredMethods0(Native Method) 
[testng] at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) 
[testng] at java.lang.Class.privateGetMethodRecursive(Class.java:3048) 
[testng] at java.lang.Class.getMethod0(Class.java:3018) 
[testng] at java.lang.Class.getMethod(Class.java:1784) 
[testng] at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544) 
[testng] at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526) 
[testng] Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.ParameterException 
[testng] at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
[testng] at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
[testng] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) 
[testng] at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
[testng] ... 7 more 
[testng] The tests failed. 

我認爲這個問題是build.xml文件(因爲我可以在IDE中運行),但無法弄清楚爲什麼這個錯誤(java.lang.NoClassDefFoundError:COM /博伊斯特/ jcommander/ParameterException)和如何解決它。我是否錯誤地指定了classfileset?我如何使這個運行?

通過其他線程,提示testng jar可能損壞,沒有jcommander jar,無效的類路徑 - 嘗試了他們,但沒有幫助。

回答

1

直接回答這個問題,似乎你必須包括

<pathelement location="dependency/jcommander-1.48.jar"/> 
main_cp

此問題重複舊版Suddenly can't run TestNG tests from ant ([testng] Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.ParameterException),其答案似乎已全部過時。

事實上,自從很久以來,jcommander並沒有包含在sonatype快照中,也沒有包含在使用github/graddle distrib生成的jar中。

因此,您必須將自己的jcommander jarfile添加到傳遞給testng ant標籤的路徑中。

你jcommander的版本似乎新舊recommended way申報TestNG的任務是:

<taskdef resource="testngtasks" classpath="testng.jar" /> 
相關問題