Can't run JUnit test cases from the command line

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

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:DevAutoTestExample
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):


cmd.exe


cd E:DevAutoTestExample
java -cp .;E:DevAutoTestExamplelibsjunit-4.12.jar;E:DevAutoTestExamplelibshamcrest-core-1.3.jar org.junit.runner.JUnitCore tests.Booking.BookingTests



I get the output



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?




2 Answers
2



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.


java -cp .


/libs/junit-4.12.jar


&



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.


/libs/junit-4.12.jar


libs


java


org.junit.runner.JUnitCore



And since you compiled your code to a different folder (.outproductionAfl_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).


.outproductionAfl_AutoTest



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.





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 at 8: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 at 14:44



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





But the class seems to be correct: tests.Booking.BookingTests (package "tests" with package "Booking" and class "BookingTests")
– Sergey Bykov
Jul 29 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
2 days ago





@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
2 days ago



-cp ".;out/production/Afl_AutoTest;libs/*.jar"


.



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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

Trying to Print Gridster Items to PDF without overlapping contents

Mass disable jenkins jobs