VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/run-junit-test-cases-from-the-command-line/

⇱ Run JUnit Test Cases From the Command Line - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Run JUnit Test Cases From the Command Line

Last Updated : 29 Sep, 2025

Unit testing is a practice in software development that ensures individual parts of an application work as expected. JUnit is the standard testing framework in Java and running tests from the command line is especially useful in CI/CD pipelines, automation scripts or environments without an IDE.

What is Unit Testing

Unit testing involves testing the smallest components of an application, typically individual methods or classes, in isolation. The goal is to verify that each function works as expected under various conditions:

  • Normal input
  • Boundary cases
  • Invalid input

Why Run JUnit Tests from the Command Line

Running JUnit tests from the command line is essential for:

  • CI/CD Integration: Automated test execution in pipelines.
  • Environment Flexibility: Useful on servers or machines without an IDE.
  • Scripted Test Runs: Continuous automated testing using shell or batch scripts.

Step-by-Step Implementation of Project

Step 1: Create a New Maven Project

Create a new Maven project using IntelliJ IDEA. Choose the following options:

  • Name: JunitExample
  • Build System: Maven

Click on the Create button.

👁 Project Metadata

Project Structure

Once created, the project structure should look like this:

👁 Project Folder Structure

Step 2: Add JUnit Dependencies to pom.xml

Open the pom.xml file and add the following dependencies to your Maven project:

Step 3: Create the StringUtility Class

Create the StringUtility class to provide methods for string manipulation.

Step 4: Main Class

Create the Main class to demonstrate the functionality of the StringUtility class.

Step 5: Write JUnit Test Cases

Create the StringUtilityTest class to write test cases for the StringUtility methods.

Step 6: Run the Application

Once the project is completed, it will run and show the below output:

👁 Output

Step 7: Run Tests Using Command Line

  • Open terminal and navigate to the project root.
  • Execute the Maven test command:

mvn test

Output:

👁 Test Output

Test Results:

👁 Test Results
Comment
Article Tags:
Article Tags:

Explore