[英]JAVA TEST java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter
[英]Running Junit in IntelliJ with Maven, getting java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
我在這里看到了幾個類似的問題,但是我不知道如何將答案應用於我的特定情況。
我試圖在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>
我覺得我很容易錯過一些簡單的事情,但現在已經搜索了很長時間,找不到答案。
謝謝!
這解決了我的問題: 如何將我的外部jar文件添加到類路徑
具體來說,這部分來自coding_idiot:
這樣,它將創建一個較大的單個jar,並且每次您嘗試構建時,構建時間都會很大。
我寧願將所有jar都添加到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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.