java - NoClassDefFoundError - RunListener : Maven-surefire/JUnit

标签 java eclipse maven junit

我一直在使用 eclipse 和 maven 学习关于 JavaEE 的 PluralSight 类(class)。我目前正在尝试编写一些测试,但是在运行 mvn package 时从命令行我收到此错误:

Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test failed: There was an error in the forked process

有时指的是:

java.lang.NoClassDefFoundError: org/junit/runner/notification/RunListener

有时:

java.lang.NoClassDefFoundError: org/junit/runner/Description

我已经阅读了一些内容,并且 RunListener 似乎已在 JUnit 4.0 中引入。但据我所知,我有正确的依赖关系 - JUnit 4.12 在我的依赖关系层次结构中。现在,我已将所有文件更改为练习文件,以便解决此问题。

我猜测也许我从根本上误解了 Maven 和依赖项的工作方式、RE jar 和类路径,并且我仍然需要将 Eclipse 或 cmd 行指向 JUnit jar? (我在两者中都遇到相同的错误)。鉴于我的 java 对象中可以识别其他 JUnit 类,我只是觉得这很奇怪。 无论如何,这是我的(pluralsight)pom:

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
             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>com.pluralsight.javaee-getting-started.javaee-getting-started-m6</groupId>
        <artifactId>bookstore-back</artifactId>
        <version>1.0</version>
        <packaging>war</packaging>
        <name>Getting Started :: javaee-getting-started-m6 :: Testing the Repository :: Back</name>

        <properties>
            <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <!-- Test -->
            <version.junit>4.12</version.junit>
            <version.arquillian>1.1.13.5</version.arquillian>
            <version.arquillian.wildfly>2.0.2.Final</version.arquillian.wildfly>
            <version.shrinkwrap>1.2.6</version.shrinkwrap>
            <!-- Plugins -->
            <version.surefire.plugin>2.19.1</version.surefire.plugin>
        </properties>

        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.arquillian</groupId>
                    <artifactId>arquillian-universe</artifactId>
                    <version>${version.arquillian}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>

        <dependencies>
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-web-api</artifactId>
                <version>7.0</version>
                <scope>provided</scope>
            </dependency>

            <!-- TEST -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${version.junit}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.arquillian.universe</groupId>
                <artifactId>arquillian-junit</artifactId>
                <scope>test</scope>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>org.jboss.shrinkwrap</groupId>
                <artifactId>shrinkwrap-api</artifactId>
                <version>${version.shrinkwrap}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.wildfly.arquillian</groupId>
                <artifactId>wildfly-arquillian-container-remote</artifactId>
                <version>${version.arquillian.wildfly}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>

        <build>
            <finalName>bookstore-back</finalName>

            <plugins>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${version.surefire.plugin}</version>
                    <configuration>
                        <systemPropertyVariables>
                            <arquillian.launch>arquillian-wildfly-remote</arquillian.launch>
                        </systemPropertyVariables>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <compilerArguments>
                            <endorseddirs>${endorsed.dir}</endorseddirs>
                        </compilerArguments>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.3</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>2.6</version>
                    <executions>
                        <execution>
                            <phase>validate</phase>
                            <goals>
                                <goal>copy</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${endorsed.dir}</outputDirectory>
                                <silent>true</silent>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>javax</groupId>
                                        <artifactId>javaee-endorsed-api</artifactId>
                                        <version>7.0</version>
                                        <type>jar</type>
                                    </artifactItem>
                                </artifactItems>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>3.0.0</version>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>src</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </plugin>
            </plugins>
        </build>

    </project>

我的类路径想法是否走在正确的轨道上?

最佳答案

删除 Maven 存储其依赖项的 .m2 文件夹,然后进行另一个构建。有时 jar 文件会损坏(例如,Junit jar 文件可能会损坏,这可以解释为什么它找不到类)。

关于java - NoClassDefFoundError - RunListener : Maven-surefire/JUnit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47679324/

相关文章:

java - 如何在 spring boot 应用程序的 .log 文件中写入 stdout 语句

java - Java 类是如何加载的,我如何确定加载类的位置/时间/原因?

eclipse - 无效 channel 117 - Eclipse/EGit

eclipse - Maven连接异常错误如何解决

java - 如何使用 maven-jar-plugin 创建带有 .class 文件的测试 jar?

java - 使用 Jenkins 进行自动化 Weblogic 服务测试

java - 尝试将 JSON(使用 GSON)读入一个具有另一个对象作为字段的对象。

java - 无法正确查看 JFace 对话框中的小部件

android - Eclipse 无法在 ASUS eee TF101 上进行调试

java - 使用整数 (1) 计算变量字符串 (010),其中结果为字符串 (011)