通过urlclassloader导入的JUnit测试用例因runnable jar而失败

时间:2013-08-30 21:21:29

标签: java eclipse selenium junit classloader

我写了一个Java应用程序,它使用URLClassLoader加载由Selenium IDE生成的JUnit测试用例,如下所示:

    public void run() {
    System.out.println("JUnit thread started");

    System.out.println("My classy: "+classy);
    JUnitCore junit = new JUnitCore();
    Result result = junit.run(classy);

classy创建如下:

    Class classy = null;

    String url2 = "file://" + junitfile.replace("\\","/");

    URL url = new URL(url2);

    URLClassLoader loader = new URLClassLoader(new URL[]{url});


    try {

        classy = loader.loadClass(classToBeLoaded);

使用Eclipse时一切正常。但是,当Java程序导出为runnable Jar并且我使Java应用程序使用JUnit类(例如test.Test1)时,这是我得到的堆栈跟踪:

test.Test1
JUnit thread started
My classy: class test.Test1
Failure count: 1
initializationError(test.Test1): org/openqa/selenium/WebDriver
java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
initializationError(test.Test1)
java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
org/openqa/selenium/WebDriver
java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at org.junit.internal.builders.SuiteMethodBuilder.hasSuiteMethod(SuiteMe
thodBuilder.java:18)
        at org.junit.internal.builders.SuiteMethodBuilder.runnerForClass(SuiteMe
thodBuilder.java:10)
        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilde
r.java:59)
        at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForC
lass(AllDefaultPossibilitiesBuilder.java:26)
        at org.junit.runner.Computer.getRunner(Computer.java:40)
        at org.junit.runner.Computer$1.runnerForClass(Computer.java:31)
        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilde
r.java:59)
        at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101)

        at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87)
        at org.junit.runners.Suite.<init>(Suite.java:80)
        at org.junit.runner.Computer.getSuite(Computer.java:28)
        at org.junit.runner.Request.classes(Request.java:75)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
        at malerisch.JUnitPanel$ThreadJUnit.run(JUnitPanel.java:315)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 19 more

org.openqa.selenium.WebDriver类包含在jar中,所以我不明白它为什么找不到它。

任何帮助都表示赞赏,如果您需要更多信息,请乐意加入。

干杯,

罗伯特

1 个答案:

答案 0 :(得分:0)

在Package Explorer中右键单击项目,转到Properties,转到Libraries选项卡,单击'Add Library'按钮,选择JUnit,然后单击Next>。你应该能够从那里处理它。

来源:java.lang.NoClassDefFoundError in junit