How To Run Junit Tests From The Command Line?

Himanshu Sheth

Posted On: November 19, 2020

view count124337 Views

Read time19 Min Read


This article is a part of our Content Hub. For more in-depth resources, check out our content hub on JUnit Tutorial.

JUnit is one of the most popular Java-based open-source frameworks used for testing every unit of the application, i.e., classes and methods. And for automating the testing of web applications, you can use it with the Selenium WebDriver. Though Selenium and JUnit can function independently, combining them can help develop tests in a structured format.

Though Java offers several IDEs using which you can write code and run tests, you would like to execute JUnit from the command line. Why?

Because it can be useful for quickly checking whether the tests are running as per expectations (avoiding the need to open the IDE). In this tutorial, we look at how to run the JUnit test from the command line. We will cover the following combinations to run JUnit tests from the command line:

  • Plain JUnit without any build-automation (or build system) tools like Apache Maven, Gradle, etc.
  • JUnit with Apache Maven
  • JUnit with Gradle
  • JUnit with ANT

It would help if you executed JUnit from the command line, especially when you want to check whether your tests are working fine or not quickly. Along with JUnit’s usage from the command line with build systems like Apache Maven and Gradle, you can use it with popular CI/CD tools like Jenkins, Bamboo, Travis CI, and more. Furthermore, delve into an extensive compilation of commonly asked JUnit Interview Questions and Answers. This resource is designed to aid interview preparation and enhance your proficiency.

The tests demonstrated in the article would be executed on the cloud-based Selenium Grid powered by LambdaTest. And, by the end of this blog, you would be in a position to execute Selenium Automation tests with JUnit outside the IDE on the command line.

For a quick recap on JUnit and Selenium WebDriver, you can refer to our blog on automated testing with JUnit and Selenium for browser compatibility.

Note – The demonstration uses JUnit 4, but if you want to run JUnit tests from the command line for JUnit 5, please chime in the comment section, and we’ll cover it for you in our subsequent blogs.

JUnitCore – Facade For Running JUnit Tests

You can run JUnit tests from the command line using the JUnit core class of the JUnit framework. The org.junit.runner.JUnitCore is an in-built class in JUnit, and it is based on the facade design pattern. The JUnitCore class provides a static method runClasses that lets you run one or several test classes. It supports running tests that use JUnit 4, JUnit 3.8.x, and its mixtures.

The runClasses method writes the feedback for all the tests and stack traces for the failed tests to complete test execution. In return, you receive an object of the type org.junit.runner.Result, which can be further used for fetching the details about the tests.

Setting Up The Build Environment To Run JUnit Tests From The Command Line

You need to download the requisite jar files necessary for compiling and running the JUnit and Selenium tests. Even though the tests will run on the cloud-based Selenium Grid, we still need to download the Selenium Server jar. It is required so that the classes offer methods in org.openqa.selenium package can be used in the implementation.

Since the requests will be executed on the cloud-based Selenium Grid, there is no necessity to start the Selenium Grid server on the machine. Along with the Selenium Grid Server jar, download the below jar files in the same directory:

Jar File
Download Link
JUnit (v 4.13.1)
https://repo1.maven.org/maven2/junit/junit/4.13.1/junit-4.13.1.jar
Selenium Server (v 4.0.0-alpha-6)
https://selenium-release.storage.googleapis.com/4.0-alpha-6/selenium-server-4.0.0-alpha-6.jar
Selenium Java (v 4.0.0-alpha-6)
https://repo1.maven.org/maven2/org/seleniumhq/selenium/selenium-java/4.0.0-alpha-6/selenium-java-4.0.0-alpha-6.jar
HamCrest (v2.2)
https://repo1.maven.org/maven2/org/hamcrest/hamcrest/2.2/hamcrest-2.2.jar

The latest version of the Hamcrest framework consists of a single jar file containing the base classes and library used for matcher implementations. Apart from Hamcrest-2.2.jar, it is not necessary to download other Hamcrest jars like Hamcrest-core.jar. Add the location where the four jar files are downloaded to the Classpath (or Path).

Now that the build setup is complete, you can compile JUnit tests and execute the tests on the terminal.

This JUnit certification establishes testing standards for those who wish to advance their careers in Selenium automation testing with JUnit.

Here’s a short glimpse of the JUnit certification from LambdaTest:

How To Compile JUnit Tests From The Command Line In Selenium

The command for compiling the JUnit test remains unchanged, irrespective of whether the JUnit test is a plain JUnit or uses a build-automation tool like Gradle, Maven, etc.

Run the following command on the terminal to compile the JUnit test:

For example, if the jar files are located in the C:\Jars folder, the following command is used for compiling the JUnit test from the command line:

How To Run JUnit Tests From The Command Line In Selenium

The command to run the JUnit test from the command line will vary depending on the build automation tool used in the project.

Here is the command to execute JUnit from the command line:

In the command, the path to the jar files (i.e., JUnit, Hamcrest, Selenium Java, and Selenium Server) is specified along with org.junit.runner.JUnitCore class that triggers the JUnit test class to run.

For example, if the jar files are located in the C:\Jars folder, the following command is used to run the JUnit test from the command line:

Different Ways To Run JUnit Tests From The Command Line

This section demonstrates how to run JUnit tests from the command line in conjunction with different build automation tools like Maven, Gradle, and Apache ANT.

Test Scenario

  1. Navigate to the URL https://lambdatest.github.io/sample-todo-app/ in the latest version of Chrome on Windows 10.
  2. Click on the web elements with li1 and li2.
  3. Add a new element to the list titled Happy Testing at LambdaTest.
  4. Assert if the new element is not added to the list.

Implementation

FileName – CrossBrowserTest.java

Code WalkThrough

Step 1: You need to import the JUnit class before annotations in the JUnit test code.

Step 2: The desired capabilities are generated using the LambdaTest capabilities generator. Since only one JUnit test has to be executed, we perform the necessary initializations in the @BeforeClass method.

Step 3: The details about the cloud-based Selenium Grid on LambdaTest along with the browser & OS combination on which the test has to be performed are passed via the capabilities object.

Step 4: The annotation @Test includes the test method i.e. test_Selenium4_ToDoApp. In the test method, web elements li1 and li2 are located using the name property.

Step 5: A new item is added to the list by locating the element with ID sampletodotext. The sendKeys method in Selenium is used for adding the text to the text box sampletodotext.

A click operation is performed on the button with the ID addbutton to add the new item to the list.

The getText method in Java Selenium is used for getting the inner text of the newly added element (i.e., Happy Testing at LambdaTest).

Now that the implementation is complete, we look at the different ways in which the JUnit test method can be run from the command line. Before we proceed with the code compilation and execution, ensure that you have downloaded all the required jar files on your machine.

junit

How To Run Plain JUnit Tests From The Command Line

The plain in the title means that we only have the test file (i.e., CrossBrowserTest.java) in the folder, and no build automation tool is used for managing the project.

Compilation of JUnit tests from the command line

The command format for the compilation of the JUnit Test is:

To compile the test file, first navigate to the location that contains the Java file and run the following command on the terminal:

Where C:\Jars contains the required Jar files for compilation.

Execution of JUnit tests from the command line

Now that the class file is generated, we run the JUnit test from the command line by triggering the following command on the terminal:

For execution, we also require to add the Hamcrest jar to the build command. The org.junit.runner.JUnitCore class triggers the JUnit test class to run.

Since the jar files in our system are located in C:\Jars, the JUnit test is run using the following command:

Here is the execution snapshot, which indicates the test was executed successfully on the Selenium Grid cloud.

Selenium Grid cloud

Selenium Grid cloud

How To Run JUnit Tests From The Command Line With Apache Maven

If you are using Maven for your JUnit project, the JUnit tests can be run on the command line using the mvn clean test command. To get started, download the latest release of Apache Maven from the download section of the Apache Maven Project site.

Apache Maven Project site

Add the location where Maven is downloaded to the Classpath using the set PATH command in Windows.

To get started with JUnit and Maven, we add the required version of JUnit to our project. In this case, the JUnit with version 4.13 is used for testing.

Instead of creating the project in Eclipse (or IntelliJ IDEA), we create a similar project structure that is required for building a Maven Project. The Junit test file (i.e. CrossBrowserTest.java) will now be a part of the org.selenium4 package. Hence, this minimal change is done to the existing test code:

FileName – CrossBrowserTest.java

The JUnit test file is placed in \src\test\java\org\selenium4.

selenium4

pom.xml is placed in the parent folder.

parent folder

Here are the contents of the pom.xml, which contains information about the project and the configuration required for building the same.

FileName – pom.xml

For a quick recap of Maven and pom.xml, you can refer to getting started with Maven for Selenium testing.

Execution Of All JUnit Tests (with Apache Maven) From The Command Line

To run all the test cases, trigger the following command on the terminal:

As seen in the execution snapshot, the packages specified in pom.xml are first downloaded on the local machine. Post downloading the packages, it executes the test org.selenium4.CrossBrowserTest on LambdaTest.

Execution Of A Particular JUnit Test (with Apache Maven) From The Command Line

In our JUnit test code, we only have one test case (i.e., CrossBrowserTest). However, an actual project might contain multiple test cases, and you may want to execute selected tests from the entire lot.

The following command is used to run a particular JUnit test on the command line:

In our case, the details are as below:

  • Package: org.selenium4
  • TestClassName: CrossBrowserTest
  • ParticularMethod: test_Selenium4_ToDoApp

Trigger the following command on the terminal to run the above JUnit test on the command line:

In case you want to forgo the generation of report files in the target\surefire-reports folder and see the stack trace, set surefire.useFile to False.

How To Run JUnit Tests From The Command Line With Gradle

If you are using Gradle for your JUnit project, you need to configure the required dependencies before running the JUnit test from the command line. Set up Gradle on Windows by downloading the installer from the official download link. Post-installation, add the location of gradle\bin to the Path (or ClassPath).

Run the gradle -v command on the terminal to verify whether Gradle was successfully installed or not.

JUnit tests

The file build.gradle is placed in the root folder of the project.

root folder

We configure the dependencies and publications in the Gradle build script (i.e., build.gradle) that define the project’s process. For building the CrossBrowserTest project, the dependencies are – Junit, Hamcrest, Selenium Chrome Driver, and Selenium Java jar. Some of these dependencies are applicable only for the ‘compile’ configuration, whereas others are applicable for the ‘testCompile’ configuration.

The dependencies under the ‘compile’ configuration (i.e., selenium-java, selenium-chrome-driver) are required for compiling the source files. The dependencies under the ‘testCompile’ configuration (i.e., Hamcrest v2.2, JUnit 4.13) are required for compiling the test files (i.e., CrossBrowserTest.java).

The dependencies under the ‘testRuntime’ configuration are required for running the tests. In our case, this configuration is not required since it includes testCompile dependencies by default.

FileName – build.gradle

Execution of all Gradle JUnit Tests From The Command Line

To run all the JUnit test cases with Gradle, trigger the following command on the terminal:

As seen in the execution snapshot, the script downloads the dependencies mentioned in build.gradle and executes the test CrossBrowserTest (which is a part of the org.selenium4 package).

CrossBrowserTest

As seen below, the JUnit test was executed successfully.

Execution Of All Gradle JUnit Tests Of A Particular Class From The Command Line

In case you want to execute JUnit from the command line of a particular class, run the following command on the terminal:

We have only one class in our project (i.e., CrossBrowserTest). Run the below command to run JUnit tests in that particular class:

Execution of a specified Gradle JUnit test of a particular Class From The Command Line

Run the following command to execute a specified JUnit test in a particular class:

The command mentioned below is used to execute test_Selenium4_ToDoApp of class CrossBrowserTest:

Here is the execution snapshot, which indicates the JUnit executed successfully:

junit-test

Testing Hyperexecute

Execution of Gradle JUnit Tests Matching A Particular Pattern From The Command Line

There are scenarios where you might want to run JUnit tests that match a name pattern. The sample command mentioned below runs the tests in the class ClassName with the method name matching *MethodName*

Trigger the below command on the terminal to execute tests in the class CrossBrowserTest with methods that match *test_Selenium4_ToDoApp*

Here is the execution snapshot, which indicates the JUnit executed successfully:

How To Run JUnit Tests From The Command Line With ANT

Apache ANT is a popular Java-based build tool. It is used for automation of build processes such as compile, run, test, and assembling of Java applications. Also, it is used for the automation of Java Selenium projects.

To get started with ANT, download the Apache ANT binary from the ANT download link. Add the location of the apache-ant\bin folder to the Path environment variable. Set the environment variable ANT_HOME to the location that contains Apache ANT. In our case, Apache ANT was installed in C:\Setup-Folder\ANT\apache-ant-1.10.9. Hence ANT_HOME is set to that folder.

ANT’s build files are written in XML, and the -f option can be used for specifying the name of the custom build file. The ANT build file is placed in the project root folder. We use the build file named build_ant.xml for building the project and running the JUnit tests in the test file.

The project dependencies are placed in the project\lib folder. We manually create a lib folder and place the required Jar files in it. The Setting Up section will guide you in downloading the Jar files needed for the JUnit project.

FileName – build_ant.xml

As seen above, the build file (build_ant.xml) contains five distinct steps (clean, makedir, compile, junit, and main). The depends attribute indicates the dependency of one target on the other. In the above build file, the target ‘compile’ depends on the execution status of the targets ‘clean’ and ‘makedir.’

The ‘property’ attribute is used to set variables, which can later be used in the build file. It consists of a name and value pair. The value of the property is accessible via ${}. For example, property named lib.dir points to the location lib.

The pathelement attribute is used for defining the classpath. The location points to the directory that contains the jar files essential for compilation and execution. Since we have stored the jar files in project-folder\lib folder, the location in pathelement should point to lib\< required-jar >.jar

The junit element in the build file is used for implementing the Apache ANT JUnit task. The task for running tests from the JUnit framework is carried out in this step.

The classpath is referenced by specifying the refid attribute on the element (i.e. in our case it is junit.class.path).

The patternset element groups different patterns into sets which can be later referenced by the id attribute. Since the test file (CrossBrowserTest.java) is stored in src/test/java/org/selenium4 folder, the same is included for the build.

Property named build-test-classes was set to ${build.dir}/org/selenium4 at the beginning of the build file where build.dir=bin.

On successful compilation, the class file is generated in the bin/org/selenium4 folder.

successful compilation

In this step, the test methods in the newly generated class file (i.e. CrossBrowserTest.class) are executed and the execution report is generated in the report folder.

selenium

Execution of ANT JUnit tests from the command line

Now that the build file is ready, we will run the JUnit test from the command line. Trigger the following command on the terminal to execute the steps in the build file (i.e., build_ant.xml).

The build command in our case is:

As seen below, the test was executed on the cloud-based Selenium Grid.

Here is the terminal snapshot, which indicates that all the ‘targets’ mentioned in the build file were completed successfully.

Shown below is the status of the test executed on the LambdaTest Grid:

LambdaTest Grid

Wrapping Up

Source
 

When using JUnit tests for your project, you may come across scenarios where you might want to run JUnit tests from the command line. This option comes in handy when you want to quickly check the JUnit units’ status, thereby avoiding the need to open IDE. In this JUnit tutorial, we looked at running the JUnit test from the command line when JUnit is used with Maven, Gradle, and ANT.

If you found this blog useful, we would be grateful if you’d share it with someone else or many others. Happy Testing ☺

Frequently Asked Questions

How do I run a JUnit test class from the command line?

Step 1: Compile the Test class by navigating to the source folder and then run the javac command.

Step 2: Run the Test by specifying the JUnit test runner (org.junit.runner.JUnitCore) and mention the class without the .java extension

How Do I Run A JUnit Test From Command Line Maven?

Step 1: From the terminal window move to the directory having the Maven project. (pom.xml file)

Step 2: Run the below command: mvn
-Dtest=UsersServiceImpl#testCreateUser test. mvn
-Dtest=UsersServiceImpl#testCreateUser test. mvn
-Dtest=UsersServiceImpl#testCreateUser test.

How Do I Run A Test Case From The Command Prompt?

You can run the test cases and test suites from the proenv command prompt.
Step 1: Run the ABLUnitCore.p driver program by passing the following as a parameter: test case, the test suite, or the folder containing test cases.
prowin32 -p ABLUnitCore.p -param testSuite.cls

Author Profile Author Profile Author Profile

Author’s Profile

Himanshu Sheth

Himanshu Sheth is a seasoned technologist and blogger with more than 15+ years of diverse working experience. He currently works as the 'Lead Developer Evangelist' and 'Senior Manager [Technical Content Marketing]' at LambdaTest. He is very active with the startup community in Bengaluru (and down South) and loves interacting with passionate founders on his personal blog (which he has been maintaining since last 15+ years).

Blogs: 128



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free