How to run JUnit tests from the command line instead of Intellij IDEA?

Viewed 1358

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?

2 Answers

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
Related