I've seen several similar questions on here but I can't figure out how to apply the answers to my particular situation.
I am trying to run JUnit in a main method in IntelliJ and using Maven. Most of the answers refer to Eclipse and not using Maven so I'm not sure how to properly do it here.
Also, in IntelliJ I can just use the gui to run the Junit tests directly with no problem, but I'm not sure if that is unrelated...
Here is the code in question that is failing (this is still in progress so obviously the first part of the program doesn't do anything too useful):
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);
}
}
Here is the error:
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
I see in other responses that I should add Junit to the classpath, but I'm not sure how to properly do this in Maven. I have it as a dependency in the pom file, so I'd think that would take care of it? For reference, here is my pom file:
<?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>
I feel like I may very easily be missing something simple, but have been searching for a long time now and can't find the answer.
Thanks!