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",
],
)
5 changes: 4 additions & 1 deletion java/private/junit5.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ See `java_junit5_test` for more details.
"""
JUNIT5_DEPS = junit5_deps()

JUNIT5_MAIN_CLASS = "com.github.bazel_contrib.contrib_rules_jvm.junit5.JUnit5Runner"

JUNIT5_VINTAGE_DEPS = junit5_vintage_deps()

def java_junit5_test(
Expand Down Expand Up @@ -100,7 +102,8 @@ def java_junit5_test(

java_test(
name = name,
main_class = "com.github.bazel_contrib.contrib_rules_jvm.junit5.JUnit5Runner",
# Use the passed main_class value or JUNIT5_MAIN_CLASS if not provided
main_class = kwargs.pop("main_class", JUNIT5_MAIN_CLASS),
test_class = clazz,
runtime_deps = runtime_deps + [
"@contrib_rules_jvm//java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5",
Expand Down