简体   繁体   English

Apache Junit:使用Junit时出现NoClassDefFoundError

[英]Apache Ant: NoClassDefFoundError when using Junit

I just can't seem to get my junit tests run by Ant. 我只是似乎无法让Ant运行我的junit测试。 Here's my setup: 这是我的设置:

build.xml http://pastebin.com/c7n4cdJ6 build.xml http://pastebin.com/c7n4cdJ6

What am I trying to do? 我想做什么? I run ant main or ant package respectively. 我分别运行ant mainant package The compile target compiles my code. compile目标编译我的代码。 First the classes of my webapp and afterwards my test classes: 首先是我的webapp的类,然后是我的测试类:

  1. javac : source: webapp java files, destination: ${web.classes.dir} javac :源:webapp java文件,目标: ${web.classes.dir}
  2. javac : source: test java files, destination: ${web.testclasses.dir} javac :源:测试Java文件,目标: ${web.testclasses.dir}

For dependency management I use Apache Ivy, which is working fine. 对于依赖项管理,我使用Apache Ivy,它运行良好。

So let's take a look at my junit target. 因此,让我们看一下我的junit目标。 You can see that the classpath is defined in junit.classpath . 您可以看到classpath是在junit.classpath定义的。 It contains: 它包含:

  1. My Ivy dependencies with the test scope: <path refid="test.path"/> 我的常春藤与测试范围的依赖关系: <path refid="test.path"/>
  2. The compiled class files of my tests: <pathelement location="${web.testclasses.dir}"/> 我的测试的已编译类文件: <pathelement location="${web.testclasses.dir}"/>
  3. The compiled class files of my webapp: <pathelement location="${web.classes.dir}"/> 我的<pathelement location="${web.classes.dir}"/>应用程序的已编译类文件: <pathelement location="${web.classes.dir}"/>
  4. Also it contains a dirset of my compiled test classes (you see I was desperate). 此外,它包含了dirset我编译的测试类(你看,我绝望了)的。

So when I run the ant script it compiles well, the class files are all there. 因此,当我运行ant脚本时,它可以很好地编译,所有类文件都在那里。 But when it comes to junit target and the first test is being executed, I get this error in the console: 但是当涉及到junit目标并且正在执行第一个测试时,我在控制台中收到此错误:

    [junit] Running CreateStopwordSqliteDBTest
    [junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec

BUILD FAILED

In the result xml file I get further information: 在结果xml文件中,我获得了更多信息:

  <error message="CreateStopwordSqliteDBTest (wrong name: de/mycompany/myproduct/CreateStopwordSqliteDBTest)" type="java.lang.NoClassDefFoundError">
    java.lang.NoClassDefFoundError: CreateStopwordSqliteDBTest (wrong name: de/mycompany/myproduct/CreateStopwordSqliteDBTest)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:191)
</error>

NoClassDefFoundError tells me that the class could be found at compile time but not on runtime. NoClassDefFoundError告诉我可以在编译时找到该类,但不能在运行时找到。 Please note that the affected class is the actual JUnit test. 请注意,受影响的类是实际的JUnit测试。 Why can't it be found even though I made sure it is part of my junit classpath? 即使我确定它是我的junit类路径的一部分,为什么也找不到它?

Fyi: When I remove the lines 131 - 133 from my build.xml (the dirset in my junit classpath), I would get a ClassNotFoundException : Fyi:当我从build.xml(我的junit类路径中的目录集)中删除第131-133 dirset ,我会得到ClassNotFoundException

  <error message="CreateStopwordSqliteDBTest" type="java.lang.ClassNotFoundException">
    java.lang.ClassNotFoundException: CreateStopwordSqliteDBTest
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:191)
</error>

Could you help me out here? 你能帮我吗?

The source directories are wrong. 源目录错误。 Here are the correct properties: 这是正确的属性:

<property name="src.dir" location="${basedir}/src" />
<property name="testsrc.dir" location="${basedir}/testsrc" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM