Maven Junit测试"noClassDefFoundError"; [英] Maven Junit tests "noClassDefFoundError"

查看:114
本文介绍了Maven Junit测试"noClassDefFoundError";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Junit测试中使用Maven.我正在使用标准的Maven项目结构,并且可以在Eclipse中运行以Junit测试运行",它们都可以成功,但是如果我要运行Maven测试/安装,则测试将无法运行,从而导致错误无法初始化" class main.sushi.persistence.Persistor".

I am using Maven with Junit tests. I am using the standard Maven project structure and I can run the "run as Junit test" in Eclipse and they all succeed but if I want to run Maven test / install then the tests are not running, resulting in the error "Could not initialize 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>

推荐答案

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

Old question but I'll provide my answer since I had a very similar issue the other day. I was writing a unittest for class Foo in package project.model which had a member of type Bar in package project.cache. When maven were to run the unittests, I got the above error on creating a mock of type Bar.

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

When running mvn verify -X, I noticed that target/classes was missing from the classpath when running the test.

我通过在pom中添加以下内容解决了该问题.

I solved it by adding the following to the 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>

这篇关于Maven Junit测试"noClassDefFoundError";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆