Maven Junit测试“noClassDefFoundError”

时间:2012-12-20 10:54:21

标签: java maven junit

我正在使用Maven和Junit测试。我正在使用标准的Maven项目结构,我可以在Eclipse中运行“作为Junit测试运行”并且它们都成功但是如果我想运行Maven测试/安装那么测试没有运行,导致错误“无法初始化class main.sushi.persistence.Persistor“。

这是我们的项目树:

├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── main
│   │   │       └── sushi
│   │   │           └── persistence
│   │   │               ├── DatabaseEnvironments.java
│   │   │               ├── META-INF
│   │   │               │   ├── persistence.xml
│   │   │               │   └── persistence_template.xml
│   │   │               └── Persistor.java
│   │   └── resources
│   └── test
│       ├── java
│       │   ├── META-INF
│       │   │   ├── persistence.xml
│       │   │   └── persistence_template.xml
│       │   └── test
│       │       └── sushi
│       │           └── persistence
│       │               ├── ProcessPersistorTest.java
│       │               └── TestPersistor.java
│       └── resources
└── target
    ├── classes
    │   ├── META-INF
    │   │   ├── MANIFEST.MF
    │   │   └── maven
    │   │       └── sushi
    │   │           └── SushiPersistence
    │   │               ├── pom.properties
    │   │               └── pom.xml
    │   └── main
    │       └── sushi
    │           └── persistence
    │               ├── DatabaseEnvironments.class
    │               ├── META-INF
    │               │   ├── persistence.xml
    │               │   └── persistence_template.xml
    │               └── Persistor.class
    ├── generated-sources
    │   └── annotations
    ├── surefire
    ├── surefire-reports
    │   ├── TEST-test.sushi.persistence.ProcessPersistorTest.xml
    │   ├── TEST-test.sushi.persistence.TestPersistor.xml
    │   ├── test.sushi.persistence.ProcessPersistorTest.txt
    │   └── test.sushi.persistence.TestPersistor.txt
    └── test-classes
        ├── META-INF
        │   ├── persistence.xml
        │   └── persistence_template.xml
        └── test
            └── sushi
                └── persistence
                    ├── ProcessPersistorTest.class
                    └── TestPersistor.class

POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sushi</groupId>
    <artifactId>SushiPersistence</artifactId>
    <version>SNAPSHOT</version>
    <repositories>
        <repository>
            <id>EclipseLink</id>
            <url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
        </repository>
    </repositories>
    <build>

        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>de.hpi-web.sushicommon</groupId>
            <artifactId>SushiCommon</artifactId>
            <version>SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.3.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa</artifactId>
            <version>2.0.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.22</version>
        </dependency>
    </dependencies>


</project>

3 个答案:

答案 0 :(得分:1)

src / main / sushi / persistence / META-INF 中的资源必须位于 src / main / resources 下,并且相应的测试文件夹必须位于 src / test / resources 而不是 src / main / java src / test / java

答案 1 :(得分:0)

从你的所有依赖项中删除范围标记..目前你已经设置了它编译,所以maven做你要求它做的事情..意味着它在测试阶段不包括你的依赖。

答案 2 :(得分:0)

旧问题,但我将提供答案,因为前几天我遇到了一个非常相似的问题。 我在project.model包中为Foo类编写了单元测试,该包在project.cache包中具有Bar类型的成员。当maven运行单元测试时,在创建Bar类型的模拟程序时出现了以上错误。

运行mvn verify -X时,我注意到运行测试时类路径中缺少目标/类。

我通过在pom中添加以下内容解决了它。

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.22.0</version>
      <configuration>
        <additionalClasspathElements>
          <additionalClasspathElement>${project.basedir}/target/classes</additionalClasspathElement>
        </additionalClasspathElements>
      </configuration>
    </plugin>

  </plugins>
</build>

相关问题