JUnit
Command Line
Testing
Software Development
Programming Tips

How to run JUnit test cases from the command line

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

JUnit is a popular unit testing framework for Java programming. Running JUnit test cases directly from the command line can be quite useful, especially when you're dealing with build scripts or continuous integration systems where you might not have the luxury of an integrated development environment (IDE) like Eclipse or IntelliJ IDEA. This article will guide you through the process of running JUnit tests from the command line using both Apache Maven and manual approaches with just Java and JUnit JARs.

Prerequisites

To run JUnit tests from the command line, ensure that you have the following installed on your system:

  • Java Development Kit (JDK)
  • JUnit jar file(s)
  • Apache Maven (optional, for a Maven-based project)

Running JUnit Tests with Maven

Apache Maven simplifies the process of project management in Java. If your project is Maven-based, it typically involves a pom.xml file at the root of the project which defines the project's dependencies and build settings.

To run JUnit tests in a Maven project:

  1. Open your command line terminal.
  2. Navigate to the root directory of your Maven project where the pom.xml file is located.
  3. Run the following command:
bash
   mvn test

Maven will handle all dependencies, compile the project, and then run the tests that are specified under the test directory typically found at src/test/java. The advantage of using Maven is that it automatically sets up the classpath and handles the instantiation of the test runner.

Running JUnit Tests Without Maven

If you are not using Maven, you will need a more manual approach. This involves downloading the required JUnit jar files and setting up the classpath manually.

Step 1: Download Required JAR Files

Download the following JAR files:

Place these JAR files in a directory, for example, C:\libs.

Step 2: Compiling Your Test Classes

Assuming your Java source files (.java) are in C:\project\src and your test files are in C:\project\test, compile them using the following command:

bash
javac -cp "C:\libs\junit-4.12.jar;C:\libs\hamcrest-core-1.3.jar" -d bin src\*.java test\*.java

This command sets the classpath to include the JUnit and Hamcrest JARs and outputs the compiled .class files to a bin directory.

Step 3: Running the JUnit Tests

Now, use the java command to run the tests:

bash
java -cp "C:\libs\junit-4.12.jar;C:\libs\hamcrest-core-1.3.jar;bin" org.junit.runner.JUnitCore [YourTestClass]

Replace [YourTestClass] with the name of your test class.

Additional Tips and Considerations

  • JUnit and Java Versions: Keep in mind that the versions of JUnit and Java should be compatible. As of my knowledge cutoff in 2023, JUnit 5 is the latest, which might have a different setup compared to JUnit 4.
  • Classpath Separators: Note that Windows uses ; while UNIX-based systems use : as the classpath separator.

Summary Table of Commands

ActionCommand
Running tests in Mavenmvn test
Compiling Java and Test classesjavac -cp "<JUnit-path>;<Hamcrest-path>" -d bin src\*.java test\*.java
Running JUnit tests without Mavenjava -cp "<JUnit-path>;<Hamcrest-path>;bin" org.junit.runner.JUnitCore [YourTestClass]

Using these instructions, you can run JUnit tests from the command line both within a Maven framework or independently, depending on your project's setup and requirements. This flexibility can greatly enhance your ability to perform continuous integration and deployment in professional software development environments.


Course illustration
Course illustration

All Rights Reserved.