简体   繁体   中英

JUnit test from commandline

I am trying to run a JUnit 4 test from commandline. This is my current command:

java -cp C:\Users\some\.m2\repository\junit\junit\4.11\junit-4.11.jar junit.textui.TestRunner C:\Some\Path\target\test-classes\com\wicket\range\ui\MyTest.class

This gives a class not found error.

I have also tried the following:

C:\Some\Path\target\test-classes>java -cp C:\Users\some\.m2\repository\junit\junit\4.11\junit-4.11.jar junit.textui.TestRunner com.wicket.range.MyTest.class

This also gives a class not found error; what could be the issue here?

I assume your test class is under C:\\Some\\Path\\target\\test-classes (in appropriate subdirectory). Your command has only junit in it. It also need class path to the test and other dependencies.

Try

java -cp C:\Some\Path\target\test-classes;C:\Users\some\.m2\repository\junit\junit\4.11\junit-4.11.jar junit.textui.TestRunner com.wicket.range.MyTest.class

Looks like you are using Maven (saw the ".m2" in your classpath). How about this..

cd <location of pom.xml>
mvn -Dtest=MyTest test

Granted it may be a while until before it runs your test and it's not going to use JUnit's text ui runner, but it should run your test without much fuss about ClassNotFoundException. Examine files in target\\surefire-reports for test results afterwards. Guess it depends on exactly what your goal is.

Otherwise, Jayan's answer looks about right to me. For his to work, I think you want to first

cd C:\Some\Path\target\test-classes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM