Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/tests_and_lints/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@ use_repo(
maven,
"maven",
)

register_toolchains(
"@@bazel_skylib//toolchains/unittest:cmd_toolchain",
"@@bazel_skylib//toolchains/unittest:bash_toolchain",
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@contrib_rules_jvm//java:defs.bzl", "checkstyle_test", "java_junit5_test")
load("@rules_jvm_external//:defs.bzl", "artifact")
load(":java_junit5_test_test_suite.bzl", "java_junit5_test_test_suite")

java_junit5_test(
name = "ExampleTest",
Expand All @@ -16,6 +17,10 @@ java_junit5_test(
],
)

java_junit5_test_test_suite(
name = "java_junit5_test_test",
)

checkstyle_test(
name = "example_test_checkstyle",
srcs = ["ExampleTest.java"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load("@contrib_rules_jvm//java:defs.bzl", "java_junit5_test")

TargetInfo = provider(
doc = "Information relating to the target under test.",
fields = ["attr"],
)

def _target_info_aspect_impl(target, ctx):
return TargetInfo(
attr = ctx.rule.attr,
)

target_info_aspect = aspect(
implementation = _target_info_aspect_impl,
)

def _attr_string_value_test_impl(ctx):
env = analysistest.begin(ctx)
target_under_test = analysistest.target_under_test(env)

asserts.equals(env, ctx.attr.check_value, getattr(target_under_test[TargetInfo].attr, ctx.attr.check_name))

return analysistest.end(env)

attr_string_value_test = analysistest.make(
_attr_string_value_test_impl,
extra_target_under_test_aspects = [target_info_aspect],
attrs = {
"check_name": attr.string(mandatory = True),
"check_value": attr.string(mandatory = True),
},
)

def java_junit5_test_test_suite(name):
java_junit5_test(
name = "StandardMainClassTest",
tags = ["manual"],
)

java_junit5_test(
name = "CustomMainClassTest",
main_class = "com.example.CustomMainClass",
tags = ["manual"],
)

attr_string_value_test(
name = "custom_main_class_test",
target_under_test = ":CustomMainClassTest",
check_name = "main_class",
check_value = "com.example.CustomMainClass",
)

attr_string_value_test(
name = "standard_main_class_test",
target_under_test = ":StandardMainClassTest",
check_name = "main_class",
check_value = "com.github.bazel_contrib.contrib_rules_jvm.junit5.JUnit5Runner",
)

native.test_suite(
name = name,
tests = [
":custom_main_class_test",
":standard_main_class_test",
],
)
4 changes: 3 additions & 1 deletion java/private/junit5.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def java_junit5_test(
exclude_tags = [],
include_engines = [],
exclude_engines = [],
main_class = "com.github.bazel_contrib.contrib_rules_jvm.junit5.JUnit5Runner",
**kwargs):
"""Run junit5 tests using Bazel.

Expand Down Expand Up @@ -73,6 +74,7 @@ def java_junit5_test(
exclude_tags: Junit tag expressions to exclude execution of tagged tests.
include_engines: A list of JUnit Platform test engine IDs to include.
exclude_engines: A list of JUnit Platform test engine IDs to exclude.
main_class: The main class to be used for the test runner.
"""
if test_class:
clazz = test_class
Expand Down Expand Up @@ -100,7 +102,7 @@ def java_junit5_test(

java_test(
name = name,
main_class = "com.github.bazel_contrib.contrib_rules_jvm.junit5.JUnit5Runner",
main_class = main_class,
test_class = clazz,
runtime_deps = runtime_deps + [
"@contrib_rules_jvm//java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5",
Expand Down