I would like to run JUnit test cases from the command line.
How can I do this?
For JUnit 5.x it's:
java -jar junit-platform-console-standalone-<version>.jar <Options>
Find a brief summary at https://stackoverflow.com/a/52373592/1431016 and full details at https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher
For JUnit 4.X it's really:
java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]
But if you are using JUnit 3.X note the class name is different:
java -cp .:/usr/share/java/junit.jar junit.textui.TestRunner [test class name]
You might need to add more JARs or directories with your class files to the classpath and separate that with semicolons (Windows) or colons (UNIX/Linux). It depends on your environment.
Edit: I've added current directory as an example. Depends on your environment and how you build your application (can be bin/ or build/ or even my_application.jar etc). Note Java 6+ does support globs in classpath, you can do:
java -cp lib/*.jar:/usr/share/java/junit.jar ...
Write tests! :-)
Maven way
If you use Maven, you can run the following command to run all your test cases:
mvn clean test
Or you can run a particular test as below
mvn clean test -Dtest=your.package.TestClassName
mvn clean test -Dtest=your.package.TestClassName#particularMethod
If you would like to see the stack trace (if any) in the console instead of report files in the target\surefire-reports folder, set the user property surefire.useFile to false. For example:
mvn clean test -Dtest=your.package.TestClassName -Dsurefire.useFile=false
Gradle way
If you use Gradle, you can run the following command to run all your test cases:
gradle test
Or you can run a particular test as below
gradle test --tests your.package.TestClassName
gradle test --tests your.package.TestClassName.particularMethod
If you would like more information, you can consider options such as --stacktrace, or --info, or --debug.
For example, when you run Gradle with the info logging level --info, it will show you the result of each test while they are running. If there is any exception, it will show you the stack trace, pointing out what the problem is.
gradle test --info
If you would like to see the overall test results, you can open the report in the browser, for example (Open it using Google Chrome in Ubuntu):
google-chrome build/reports/tests/index.html
Ant way
Once you set up your Ant build file build.xml, you can run your JUnit test cases from the command line as below:
ant -f build.xml <Your JUnit test target name>
You can follow the link below to read more about how to configure JUnit tests in the Ant build file:
https://ant.apache.org/manual/Tasks/junit.html
Normal way
If you do not use Maven, or Gradle or Ant, you can follow the following way:
First of all, you need to compile your test cases. For example (in Linux):
javac -d /absolute/path/for/compiled/classes -cp /absolute/path/to/junit-4.12.jar /absolute/path/to/TestClassName.java
Then run your test cases. For example:
java -cp /absolute/path/for/compiled/classes:/absolute/path/to/junit-4.12.jar:/absolute/path/to/hamcrest-core-1.3.jar org.junit.runner.JUnitCore your.package.TestClassName
The answer that #lzap gave is a good solution. However, I would like to add that you should add . to the class path, so that your current directory is not left out, resulting in your own classes to be left out. This has happened to me on some platforms. So an updated version for JUnit 4.x would be:
java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]
Ensure that JUnit.jar is in your classpath, then invoke the command line runner from the console
java org.junit.runner.JUnitCore [test class name]
Reference: junit FAQ
With JUnit 4.12 the following didn't work for me:
java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]
Apparently, from JUnit 4.11 onwards you should also include hamcrest-core.jar in your classpath:
java -cp .:/usr/share/java/junit.jar:/usr/share/java/hamcrest-core.jar org.junit.runner.JUnitCore [test class name]
In windows it is
java -cp .;/path/junit.jar org.junit.runner.JUnitCore TestClass [test class name without .class extension]
for example:
c:\>java -cp .;f:/libraries/junit-4.8.2 org.junit.runner.JUnitCore TestSample1 TestSample2 ... and so on, if one has more than one test classes.
-cp stands for class path and the dot (.) represents the existing classpath while semi colon (;) appends the additional given jar to the classpath , as in above example junit-4.8.2 is now available in classpath to execute JUnitCore class that here we have used to execute our test classes.
Above command line statement helps you to execute junit (version 4+) tests from command prompt(i-e MSDos).
Note: JUnitCore is a facade to execute junit tests, this facade is included in 4+ versions of junit.
If your project is Maven-based you can run all test-methods from test-class CustomTest which belongs to module 'my-module' using next command:
mvn clean test -pl :my-module -Dtest=CustomTest
Or run only 1 test-method myMethod from test-class CustomTest using next command:
mvn clean test -pl :my-module -Dtest=CustomTest#myMethod
For this ability you need Maven Surefire Plugin v.2.7.3+ and Junit 4.
More details is here: http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html
Actually you can also make the Junit test a runnable Jar and call the runnable jar as
java -jar
Personally I would use the Maven surefire JUnit runner to do that.
Alternatively you can use the following methods in JunitCore class http://junit.sourceforge.net/javadoc/org/junit/runner/JUnitCore.html
run (with Request , Class classes and Runner) or runClasses from your java file.
Related
I have an Intellij IDEA project with Junit5 tests in the tests folder. The tests works if I click on the play button using IDEA's GUI. However, I need to be able to use the command line to run those tests but I don't know how.
Given the project folder how can I use the command line to run the tests in the tests folder without relying on IDEA's GUI?
You can use the JUnit ConsoleLauncher:
The ConsoleLauncher is a command-line Java application that lets you launch the JUnit Platform from the console. For example, it can be used to run JUnit Vintage and JUnit Jupiter tests and print test execution results to the console.
To run all tests from a package you can run:
java -jar junit-platform-console-standalone-1.5.2.jar
-cp 'build/classes/java/test'
--select-package com.mypackage
You can read the tutorial from MKyong for an introduction: JUnit 5 ConsoleLauncher examples
Most build systems provide test targets. For example:
Gradle: ./gradlew test
Maven: mvn test
I have two main classes in my Maven based project (Spring Boot). I'm using <start-class> property in pom.xml to indicate the Spring Boot main class. The other one is a process that I need to run manually sometimes.
This is the project's structure:
Project (pom.xml dir) is in ~/projects/coolproject
Packages (containing the classes) are under ~/projects/coolproject/src/main/java
Main class I want to execute manually is ~/projects/coolproject/src/main/java/com/company/Process.java
After run mvn package, I want to run Process.class, but I'm getting Could not find or load main class error. I have tried this:
From ~/projects/coolproject/src/main/java, execute java com.company.Process
From ~/projects/coolproject/src/main/java, execute java -cp . com.company.Process
From ~/projects/coolproject/src/main/java/com/company, execute java Process
From ~/projects/coolproject/, execute java -cp target/coolproject-1.0.0-SNAPSHOT.jar com.company.Process
with no success. How can I do it?
I have tried to run it using Maven Exec plugin. From pom.xml's dir:
$ mvn exec:java -Dexec.mainClass="com.company.Process"
But it runs the other main class (Spring Boot's main class), instead. It's ignoring the mainClass param.
As mentioned in comments, while using spring boot, you should use start-class instead of mainClass as the parameter in exec plugin. Try:
mvn exec:java -Dstart-class=com.company.Process
Alternatively, if you want to use java -cp to run your main class, you need to pass the correct dependencies. It seems that coolproject-1.0.0-SNAPSHOT.jar isn't built with dependencies that com.company.Process needs. To build a jar with all dependencies, you can use maven-assembly-plugin. Then you can try:
mvn package
java -cp target/coolproject-1.0.0-SNAPSHOT-jar-with-dependencies.jar com.mycompany.Process
This is what i have done till now:
I have API automation Script in maven using testng, Following is the structure of project
2.Now i first tried to run testng.xml using command-line with following command.
java -cp ".;C:\Users\A622965\.m2\repository\org\testng\testng\6.8\testng-6.8.jar" org.testng.TestNG testng.xml
3.But throws following error:
Error: Could not find or load main class org.testng.TestNG
Not able to figure out the issue after watching lot of tutorials.
I am looking to batch process the script using Task Schedular in Windows
Remember that Maven is a build tool, so every single operation which requires the classpath must be done through Maven, wether if you want to execute it from the command line or from some GUI.
So, in your case you will find useful the Maven command line tool:
mvn <phases>
In your case:
mvn test
But remember to include first in the pom.xml the testng library dependency, and also to properly configure the surefire plugin.
You need to build path for testng
Right click-->Build -->Libraries Tab-->Add External jar-->Then restart--it will work...
I am using linux mechine(ubuntu 14). my project is maven project with testng frame work. here i am trying to run the testng.xml from the command prompt so that i can configure in jenkins.
project location : /home/subhash/workspace/myproject
class path is : /home/subhash/workspace/myproject/bin
testng jar locattion is: /home/subhash/.m2/repository/org/testng/testng/6.9.4/testng-6.9.4.jar"
suite location is : /home/subhash/workspace/suites/src/test/resources/testng.xml
hence i executed the following command from command prompt from the location.
/home/subhash/workspace/myproject/bin$
$ java -cp ".:../lib/*:/home/subhash/.m2/repository/org/testng/testng/6.9.4/testng-6.9.4.jar" org.testng.TestNG /home/subhash/workspace/suites/src/test/resources/testng.xml
i got the following Exception:
Exception in thread "main" java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException
hence i have added the dependency in pom.xml as follows.
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.48</version>
build it. it was successful.
again i have run the above mentioned command to run the testng.xml but still same exception is displayed.
Please help me in this issue.
Thanks in advance.
Subhash.
Take a look at this question: Suddenly can't run TestNG tests from ant ([testng] Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.ParameterException)
It seems to be the same problem
Hope helps!
UPDATE
Try to add to the cp java argument the 'beust' jar path in m2.
It would be easier running the tests using mvn test instead of the testng jar.
Setup the mvn surefire plugin in pom and run using mvn test -DsuiteXmlFile=src/test/resources/yourxml.xml
I have JUnit test classes in both the default Java package and my specific package. My Gradle build-file contains
test {
include 'edu/ucar/unidata/sruth/'
}
in order to exclude the JUnit test classes in the default Java package. It's not working: a gradle test always executes the default Java package tests as well as the package-specific tests (according to the contents of build/reports/test).
How do I execute only the package-specific tests?
I'm using Gradle 1.0.
ADDENDUM: Executing gradle cleanTest fixed the problem.
I extracted following segment from Gradle official doc, hope it helps http://www.gradle.org/docs/current/userguide/java_plugin.html
Some examples of using the command line option:
gradle test --tests org.gradle.SomeTest.someSpecificFeature
gradle test --tests *SomeTest.someSpecificFeature
gradle test --tests *SomeSpecificTest
gradle test --tests all.in.specific.package*
gradle test --tests *IntegTest
gradle test --tests *IntegTest*ui*
gradle someTestTask --tests *UiTest someOtherTestTask --tests
*WebTest*ui
not sure what JUNIt is but if you have two classes with the same name you have to specify which one you are talking about like... import java.util.Scanner; and to use it you would declare it as java.util.Scanner foo=new java.util.Scanner();