0

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?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328

2 Answers2

1

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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thank you for answer, i tried this variant too, but if i use quotes in PS I get error output: "Could not find or load main class org.junit.runner.JUnitCore" – Sergey Bykov Jul 29 '18 at 08:31
  • Yeah, thx, now junit is trying to start the tests, but as i wrote ealier(about cmd.exe) I get the output `java.lang.IllegalArgumentException: Could not find class [tests.Booking.BookingTests] Caused by: java.lang.ClassNotFoundException: tests.Booking.BookingTests` – Sergey Bykov Jul 29 '18 at 14:44
  • But the class seems to be correct: tests.Booking.BookingTests (package "tests" with package "Booking" and class "BookingTests") – Sergey Bykov Jul 29 '18 at 15:45
  • Thank you! the final command for my project must include all "libs" folder files: 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 – Sergey Bykov Jul 30 '18 at 11:18
  • @SergeyBykov You should be able to simplify that to `-cp ".;out/production/Afl_AutoTest;libs/*.jar"`. You can probably even remove the `.` from the classpath, since there don't seem to be any libraries or class files in your projec root (at least there shouldn't be). – Ansgar Wiechers Jul 30 '18 at 11:48
0

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