Sergey Bykov
Sergey Bykov

Reputation: 31

Can't run JUnit test cases from the command line

I'm trying to run tests from the command line (PowerShell in Windows 10). Before asking this question I looked in several sources and read a lot of topics, like

But when I'm trying to run tests from PowerShell like in JUnit wiki

cd E:\Dev\AutoTest\Example
java -cp .;/libs/junit-4.12.jar;/libs/hamcrest-core-1.3.jar org.junit.runner.JUnitCore tests.Booking.BookingTests

I get the output

CommandNotFoundException


If I run the same, but via old command line (cmd.exe):

cd E:\Dev\AutoTest\Example
java -cp .;E:\Dev\AutoTest\Example\libs\junit-4.12.jar;E:\Dev\AutoTest\Example\libs\hamcrest-core-1.3.jar org.junit.runner.JUnitCore tests.Booking.BookingTests

I get the output

java.lang.IllegalArgumentException: Could not find class [tests.Booking.BookingTests]  
    Caused by: java.lang.ClassNotFoundException: tests.Booking.BookingTests

In IDEA the project structure look like this:

IDEA project structure

On the hard drive the structure look like this:

hard drive project structure

• "out" folder cointains *.class files

• "src" folder contains *.java files


The question:

How to run JUnit test cases from the command line in PowerShell with my structure?

Upvotes: 0

Views: 2187

Answers (2)

Sergey Bykov
Sergey Bykov

Reputation: 31

Final comand must include all "libs" folder files:

• java-client-4.1.2.jar

• junit-4.12.jar

• selenium-server-standalone-3.4.0.jar

• hamcrest-core-1.3.jar

java -cp ".;out/production/Afl_AutoTest;libs/java-client-4.1.2.jar;libs/junit-4.12.jar;libs/selenium-server-standalone-3.4.0.jar;libs/hamcrest-core-1.3.jar" org.junit.runner.JUnitCore tests.Booking.BookingTests

if not, the output will be

Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/Capabilities

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200503

In PowerShell the semicolon is a command separator (allowing you to put two statements on one line), so you're running java -cp . (which should output the command help) and then /libs/junit-4.12.jar (which is not recognized as a command). The example in the JUnit wiki clearly isn't made for PowerShell, but for CMD, which uses the ampersand (&) for chaining commands, so the issue doesn't occur there.

Also, you made the paths in the classpath absolute (/libs/junit-4.12.jar), but your libs directory is in your project folder. That is why java complains that it can't find the class org.junit.runner.JUnitCore. When you're running JUnit from the root of your project directory you need to make the paths relative to that location.

And since you compiled your code to a different folder (.\out\production\Afl_AutoTest) you must add that folder to your classpath as well, otherwise JUnit won't be able to find the compiled classes (because they're outside the classpath).

Put your classpath in quotes, add the output directory, and remove the leading slashes from the library paths, and the command should work in CMD and PowerShell alike:

java -cp ".;out/production/Afl_AutoTest;libs/junit-4.12.jar;libs/hamcrest-core-1.3.jar" org.junit.runner.JUnitCore tests.Booking.BookingTests

Better yet, define all arguments as an array and splat it:

$classpath = '.',
             'out/production/Afl_AutoTest',
             'libs/junit-4.12.jar',
             'libs/hamcrest-core-1.3.jar'
$params    = '-cp', ($classpath -join ';'),
             'org.junit.runner.JUnitCore',
             'tests.Booking.BookingTests'

java @params

The latter only works in PowerShell, though.

Upvotes: 1

Related Questions