- Bazel does not work with JUnit5
When you specify the JUnit4 option
--test_filter
, it actually runs all tests, and also you can not specify the JUnit5 option--test_args
.
# in JUnit4 style, would run all tests
bazel test //...:test "--test_filter=test.package.TestClass#testMethod"
# in JUnit5 style, would be rejected by Bazel
bazel test //...:test "--test_arg=--select-method=test.package.TestClass#testMethod"
This breaks your agile development in a big project.
- Bazel invokes your tests in JUnit4's style by a specific environment variable:
TESTBRIDGE_TEST_ONLY=test.package.TestClass#testMethod # test by select-method
TESTBRIDGE_TEST_ONLY=test.package.TestClass # test by select-class
TESTBRIDGE_TEST_ONLY=test.package # test by select-package
- For JUnit5, the arguments should be:
--test_arg=--select-method=test.package.TestClass#testMethod
--test_arg=--select-class=test.package.TestClass
--test_arg=--select-package=test.package
- Set the main_class and deps for java_junit5_test
java_junit5_test(
# ...
main_class = "com.flexport.bazeljunit5.BazelJUnit5ConsoleLauncher",
deps = [
"//bazeljunit5/src/main/java/com/flexport/bazeljunit5",
# ...
]
)
- Run a single test
- from command line
bazel test //...:test "--test_filter=com.example.MyTest#test"
- or right click on a single test in a
*Test.java
file and and select "Run ..." in your IDE (e.g. IntelliJ IDEA).