私はここでいくつかの同様の質問を見ましたが、私の特定の状況に答えを適用する方法を理解できません。MavenでIntelliJでJunitを実行し、java.lang.NoClassDefFoundErrorを取得する:org/junit/runner/JUnitCore
私はIntelliJのメインメソッドでMavenを使用してJUnitを実行しようとしています。回答のほとんどはEclipseを参照し、Mavenを使用しないので、ここで正しく行う方法がわかりません。
また、IntelliJの中で私は問題なく直接JUnitテストを実行するためのGUIを使用することができますが、それは無関係である場合、私はわからないんだけど...ここ
が失敗している問題のコードです(これはとても明らかに、プログラムの最初の部分があまりにも何も有効ではありませんまだ進行中である):ここでは
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ファイルに依存しているので、それを処理すると思いますか?私は非常に簡単な何かが欠落している可能性がありますが、今長い時間を探していると答えを見つけることができないように私は感じ
<?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>
:参考のために、ここに私のPOMファイルです。
ありがとうございます!
どのようにプログラムを実行していますか? JUnit.jarは実際にそのクラスパス上にある必要があります。 – MeBigFatGuy
応答のために@MeBigFatGuyに感謝します。私はしていた: 'mvnクリーンインストール'に続いて 'java -jar target/StatusCodeValidator-1.0-SNAPSHOT.jar TestUrls.csv' –
コメントのために@MeBigFatGuyをありがとう。あなたの回答を読んで、さらに多くの研究を続けながら、私は以下の指示に従って解決することができました:http://stackoverflow.com/questions/18414812/how-to-add-my-external-jar-file-to -class-path –