Main Tutorials

JUnit 5 ConsoleLauncher examples

This article shows you how to use the JUnit 5 ConsoleLauncher to run tests from a command line.

Tested with

  • JUnit 5.5.2
  • junit-platform-console-standalone 1.5.2

1. Download JAR

To run tests from the command line, we can download the junit-platform-console-standalone.jar from Maven central repository manually.

P.S This example is using version 1.5.2.

https://repo1.maven.org/maven2/org/junit/platform/junit-platform-console-standalone/1.5.2/
run tests from console

2. Usages

Usually, the test classes are at the following classpath:

  • build/classes/java/test
  • target/test-classes

2.1 Run all tests from this classpath build/classes/java/test

Terminal

$ java -jar junit-platform-console-standalone-1.5.2.jar --classpath build/classes/java/test --scan-classpath

$ java -jar junit-platform-console-standalone-1.5.2.jar -cp build/classes/java/test --scan-classpath

2.2 Run a specified test with the class name.

Terminal

$ java -jar junit-platform-console-standalone-1.5.2.jar 
	-cp 'build/classes/java/test' 
	-c com.mkyong.order.MethodOrderTest

$ java -jar junit-platform-console-standalone-1.5.2.jar 
	-cp 'build/classes/java/test' 
	--select-class com.mkyong.order.MethodOrderTest

2.3 Run all tests from a package.

Terminal

$ java -jar junit-platform-console-standalone-1.5.2.jar 
	-cp 'build/classes/java/test' 
	--select-package com.mkyong.order

2.4 Run all tests from a package, filter the tests’ class name via a regex pattern.

Terminal

$ java -jar junit-platform-console-standalone-1.5.2.jar 
	-cp 'build/classes/java/test' 
	--select-package com.mkyong.order 
	--include-classname='.*Count.*'

2.5 Output sample.

Terminal

$ java -jar junit-platform-console-standalone-1.5.2.jar 
	-cp 'build/classes/java/test' 
	--select-package com.mkyong.order 
	--include-classname='.*'

Thanks for using JUnit! Support its development at https://junit.org/sponsoring

+-- JUnit Jupiter [OK]
| +-- MethodRandomTest [OK]
| | +-- testA() [OK]
| | +-- testZ() [OK]
| | +-- testY() [OK]
| | +-- testE() [OK]
| | '-- testB() [OK]
| +-- MethodParameterCountTest [OK]
| | +-- Parameter Count : 3 [OK]
| | | +-- 1 ==> fruit='apple', qty=1, price=1.99 [OK]
| | | '-- 2 ==> fruit='banana', qty=2, price=2.99 [OK]
| | +-- Parameter Count : 2 [OK]
| | | +-- 1 ==> fruit='apple', qty=1 [OK]
| | | '-- 2 ==> fruit='banana', qty=2 [OK]
| | '-- Parameter Count : 1 [OK]
| |   +-- 1 ==> ints=1 [OK]
| |   +-- 2 ==> ints=2 [OK]
| |   '-- 3 ==> ints=3 [OK]
| +-- MethodAlphanumericTest [OK]
| | +-- testA() [OK]
| | +-- testB() [OK]
| | +-- testE() [OK]
| | +-- testY() [OK]
| | '-- testZ() [OK]
| '-- MethodOrderTest [OK]
|   +-- test2() [OK]
|   +-- test3() [OK]
|   +-- test1() [OK]
|   +-- test0() [OK]
|   '-- test4() [OK]
'-- JUnit Vintage [OK]

Test run finished after 109 ms
[         9 containers found      ]
[         0 containers skipped    ]
[         9 containers started    ]
[         0 containers aborted    ]
[         9 containers successful ]
[         0 containers failed     ]
[        22 tests found           ]
[         0 tests skipped         ]
[        22 tests started         ]
[         0 tests aborted         ]
[        22 tests successful      ]
[         0 tests failed          ]

P.S Refer to the official JUnit 5 guide for all the ConsoleLauncher options

3. Windows

3.1 If we want to run the above commands in the classic command prompt, replace the single quote with double quotes, for example:

Command Prompt – Windows

$ java -jar junit-platform-console-standalone-1.5.2.jar 
	-cp "build/classes/java/test"
	-c com.mkyong.order.MethodOrderTest

3.2 Pass a --disable-ansi-colors option to disable ANSI colors in the output. The command prompt does not support this feature.

Command Prompt – Windows

$ java -jar junit-platform-console-standalone-1.5.2.jar 
	-cp "build/classes/java/test"
	-c com.mkyong.order.MethodOrderTest
	--disable-ansi-colors

Download Source Code

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Garry
4 years ago

I’m using a gradle project. The test classes are under “build/classes/java/test”. But my project has a lot of external dependencies managed by Gradle.

I’ve already tried to set gradle repository as classpath dependency but i wont be able to make it works.

Could you add an example using external libraries?

shawshank
3 years ago

可以直接对java文件代码测试吗