【问题标题】:Running Junit in IntelliJ with Maven, getting java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore使用 Maven 在 IntelliJ 中运行 Junit,得到 java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
【发布时间】:2017-01-05 20:18:21
【问题描述】:

我在这里看到了几个类似的问题,但我不知道如何将答案应用于我的特定情况。

我正在尝试在 IntelliJ 的主要方法中运行 JUnit,并使用 Maven。大多数答案都是指 Eclipse 而不是使用 Maven,所以我不确定如何在这里正确地做到这一点。

另外,在 IntelliJ 中,我可以直接使用 gui 直接运行 Junit 测试,但我不确定这是否无关......

这是失败的代码(这仍在进行中,因此显然程序的第一部分没有做任何太有用的事情):

package validator;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;

public class RunValidator {
    public static void main (String[] args) {
        String testDataPath;

        if (args.length == 1) {
            testDataPath = args[0];
        } else {
            System.out.println("Please enter an argument (only one) for the test path data.");
            System.exit(0);
        }

        Result result = JUnitCore.runClasses(Validator.class);
        System.out.println(result);
    }
}

这是错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
        at validator.RunValidator.main(RunValidator.java:22)
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more

我在其他回复中看到我应该将 Junit 添加到类路径中,但我不确定如何在 Maven 中正确执行此操作。我将它作为 pom 文件中的依赖项,所以我认为这会解决它?作为参考,这是我的 pom 文件:

<?xml version="1.0" encoding="UTF-8"?>
<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>validation</groupId>
    <artifactId>StatusCodeValidator</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>htmlunit-driver</artifactId>
            <version>2.23.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-support</artifactId>
            <version>3.0.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.5.0</version>
                <configuration>
                    <mainClass>com.example.Main</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>validator.RunValidator</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

</project>

我觉得我可能很容易错过一些简单的东西,但是现在已经搜索了很长时间,但找不到答案。

谢谢!

【问题讨论】:

  • 你是如何运行你的程序的。 JUnit.jar 确实需要在该类路径上。
  • 感谢@MeBigFatGuy 的回复。我在做:mvn clean install,然后是java -jar target/StatusCodeValidator-1.0-SNAPSHOT.jar TestUrls.csv
  • @MeBigFatGuy 感谢您的评论。在阅读了您的回复后,仍然进行了越来越多的研究,我能够按照以下方向解决它:stackoverflow.com/questions/18414812/…

标签: java maven intellij-idea junit


【解决方案1】:

这解决了我的问题:how to add my external jar file to class path

具体来说这部分来自coding_idiot:

这样它会创建一个大尺寸的单罐,每次你尝试构建时构建时间都会很长。

我更喜欢将所有 jars 添加到 lib 文件夹并包含在类路径(jar 的清单)中,因为当我们必须进行一些更改或重新部署到客户端或某个地方时,我们可以简单地提供小 jar (并非所有依赖项都合并在 jar 中)

      <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.kalindiinfotech.webcrawler.MainGUI</mainClass>
                        <!--                            <mainClass>com.KalindiInfotech.busbookingmaven.form.LoginForm</mainClass>-->
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

【讨论】:

  • 我用上面的 sn-p 试过了,我肯定错过了一些东西。它不工作。我也是 Maven 新手,我尝试重建项目(Eclipse),它仍然是一样的。
  • 嗨@hjchin。很遗憾听到这不起作用。我现在有更多的 maven 经验,所以也许我可以帮忙:) 你能提供你的整个 pom 文件以及你正在运行什么命令吗?这可能会有所帮助。一定要尝试做一个mvn clean,或者如果Eclipse 有任何类型的“maven reimport”特性,试试那个。有时这可以帮助解决问题。
  • 嗨@Michael,我设法解决了它。我从这个 sn-p 中删除了 标记。我认为它限制了范围测试的使用。不过,我没有进一步检查范围的使用情况。 junitjunit4.12test
猜你喜欢
  • 2019-11-24
  • 2020-05-13
  • 2018-06-11
  • 1970-01-01
  • 2019-06-06
  • 2018-09-13
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多