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 Structure
Once created, the project structure should look like this:

Step 2: Add JUnit Dependencies to pom.xml
Open the pom.xml file and add the following dependencies to your Maven project:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gfg</groupId>
<artifactId>JUnitExample</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- JUnit 5 dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
</project>
Step 3: Create the StringUtility Class
Create the StringUtility class to provide methods for string manipulation.
package com.gfg;
public class StringUtility {
public String reverse(String input) {
if (input == null) throw new IllegalArgumentException("Input cannot be null");
return new StringBuilder(input).reverse().toString();
}
public String toUpperCase(String input) {
if (input == null) throw new IllegalArgumentException("Input cannot be null");
return input.toUpperCase();
}
public int countVowels(String input) {
if (input == null) throw new IllegalArgumentException("Input cannot be null");
int count = 0;
for (char c : input.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(c) >= 0) count++;
}
return count;
}
}
Step 4: Main Class
Create the Main class to demonstrate the functionality of the StringUtility class.
package com.gfg;
public class Main {
public static void main(String[] args) {
StringUtility stringUtility = new StringUtility();
System.out.println("Reversed: " + stringUtility.reverse("example"));
System.out.println("Uppercase: " + stringUtility.toUpperCase("example"));
System.out.println("Vowel count: " + stringUtility.countVowels("example"));
}
}
Step 5: Write JUnit Test Cases
Create the StringUtilityTest class to write test cases for the StringUtility methods.
import com.gfg.StringUtility;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class StringUtilityTest {
private final StringUtility stringUtility = new StringUtility();
@Test
void testReverse() {
assertEquals("tac", stringUtility.reverse("cat"));
assertEquals("", stringUtility.reverse(""));
assertThrows(IllegalArgumentException.class, () -> stringUtility.reverse(null));
}
@Test
void testToUpperCase() {
assertEquals("HELLO", stringUtility.toUpperCase("hello"));
assertEquals("WORLD", stringUtility.toUpperCase("WoRlD"));
assertThrows(IllegalArgumentException.class, () -> stringUtility.toUpperCase(null));
}
@Test
void testCountVowels() {
assertEquals(3, stringUtility.countVowels("example"));
assertEquals(0, stringUtility.countVowels("xyz"));
assertThrows(IllegalArgumentException.class, () -> stringUtility.countVowels(null));
}
}
Step 6: Run the Application
Once the project is completed, it will run and show the below 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 Results:
