【问题标题】:NoClassDefFoundError - RunListener : Maven-surefire / JUnit [duplicate]NoClassDefFoundError - RunListener:Maven-surefire / JUnit [重复]
【发布时间】:2017-12-06 16:41:50
【问题描述】:

我一直在学习有关使用 eclipse 和 maven 的 JavaEE 的 PluralSight 课程。我目前正在尝试编写一些测试,但是当从 cmd 行运行 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

我做了一些阅读,似乎在 JUnit 4.0 中引入了 RunListener。但据我所知,我有正确的依赖关系——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 文件可能已损坏,这可以解释为什么它找不到类)。还运行mvn dependency-tree 以检查您是否确实拥有正确的版本(尽管根据您的 pom 应该没问题)。此外,您所有的测试都在src/test/java 下,对吗?
  • 删除了我的 .m2 文件并重新打包——我认为你解决了这个问题——它标记了一个较旧的问题。不过还是谢谢!我现在得到运行时异常could not connect to http-remoting://127.0.0.1:9990。这虽然很烦人,但至少是我在尝试运行jboss-cli.bat --connect --controller=localhost:9990 时遇到的错误。我以前在使用此命令启动 WildFly 时遇到问题(standalone.bat 但是工作正常。)
  • 运行 mvn dependency:tree 导致:Execution default-cli of goal org.apache.maven.plugins:maven-dependency-plugin:2.6:tree failed: A required class was missing while executing org.apache.maven.plugins:maven-dependency-plugin:2.6:tree: org/sonatype/aether/graph/DependencyNode
  • org/sonatype/aether/graph/DependencyNode 这是由一个非常老的 maven-dependency-plugin 引起的。您至少需要 2.8 版。见:stackoverflow.com/questions/18169415/… 关于WildFly,我不太熟悉,建议再问一个问题。
  • 还根据我们的对话发布了答案。

标签: java eclipse maven junit


【解决方案1】:

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

【讨论】:

  • 在你猜的时候,为什么不建议格式化硬盘,重新安装操作系统和其他一切,或者你没有完整的答案
  • 在尝试回答更多问题之前,请阅读How do I write a good answer?
  • @JarrodRoberson 我的回答基于我在这个问题的评论部分与 OP 的对话。它解决了OP的问题。不回答这个问题会更好吗?
  • 改写了答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-06
  • 1970-01-01
  • 2016-02-29
  • 2015-01-28
  • 2020-12-31
相关资源
最近更新 更多