Test your Rego with JUnit
rego4k-testing-junit makes each Rego test a JUnit test, so Gradle, Maven and the IDE run it and show it next to the
Kotlin tests. The outcomes are the outcomes of opa test.
Teams write Rego tests for their policies: rules whose names start with test_. opa test runs them, apart
from the build of the application.
Each block of Kotlin on this page is a part of the complete test class
PolicyTests in site/examples.
Add the dependencies
dependencies {
implementation(platform("dev.rego4k:rego4k-bom:$rego4kVersion"))
implementation("dev.rego4k:rego4k-core")
implementation("dev.rego4k:rego4k-builtins-stdlib-pure")
// the regex engine of OPA
implementation("dev.rego4k:rego4k-builtins-re2")
testImplementation("dev.rego4k:rego4k-testing-junit")
testImplementation("org.junit.jupiter:junit-jupiter")
}BuildFilesTest.kt, lines 76 to 84, on GitHubWrite the Rego tests
Put the tests next to the policies, as for opa test. These are the tests of the policy authz.rego of the
guides:
package authz_test
import data.authz
test_an_admin_can_delete if {
authz.allow with input as {"method": "DELETE", "user": {"roles": ["admin"]}}
}
test_a_viewer_cannot_delete if {
not authz.allow with input as {
"method": "DELETE",
"user": {"roles": ["viewer"]},
}
}
test_a_viewer_can_read if {
authz.allow with input as {"method": "GET", "user": {"roles": ["viewer"]}}
}authz_test.rego, lines 2 to 19, on GitHub- A test rule with test cases (
test_sums[tc.note] if { ... }) is a container in JUnit, with a test for each case. - A rule whose name starts with
todo_test_is a skipped test.
Run them with JUnit
RegoTests.junit runs the tests of the files at the paths, as opa test <path> does, and gives JUnit 5 dynamic
tests to a @TestFactory:
@TestFactory
fun policies() = RegoTests.junit(
Rego.standard(Re2Engine),
File("src/main/kotlin/rego4k/examples/policies"),
)PolicyTests.kt, lines 24 to 28, on GitHub- Each package is a container, with a dynamic test for each test rule.
- A failure or an error fails its dynamic test. The message is the text of
opa test -vfor the test: theprintoutput, the error, and the notes and the failures of the trace. - The source of each test is the file and the line of the rule, so the IDE goes to the rule from the test.
- If a module does not parse or compile, the factory throws the error.
To run the tests of a bundle, with its manifest, give a TestRunner and the bundle. With traced = true, a
failure shows the trace:
@TestFactory
fun bundle() = RegoTests.junit(
TestRunner(Rego.standard(Re2Engine), traced = true),
PolicyFiles.bundle(Path.of("src/main/kotlin/rego4k/examples/policies")),
)PolicyTests.kt, lines 32 to 36, on GitHubFix the time of a test
A policy that reads the time gives a different result on another day. Give the engine a fixed clock, so that the test gives the same result on each day.
time.now_ns is a provided builtin, so add Builtins.Provided.stdlib.
package hours
working_hours if {
[hour, _, _] := time.clock([time.now_ns(), "Europe/London"])
hour >= 9
hour < 18
}
test_working_hours if working_hourshours.rego, lines 2 to 10, on GitHub@TestFactory
fun hours() = RegoTests.junit(
Rego(
Builtins.Pure.stdlib(Re2Engine) + Builtins.Provided.stdlib,
clock = InstantSource.fixed(Instant.parse("2026-01-15T10:00:00Z")),
),
File("src/test/kotlin/rego4k/examples/junit/hours"),
)PolicyTests.kt, lines 40 to 47, on GitHubSee A query result can be reproduced.
Run them on the command line
rego4k test, the test command of rego4k's command line, runs the tests as
opa test does, with the text and the exit codes of opa test.
From the repository, with just, a command runner:
just test-rego -v ./policiesA test of rego4k runs 45 opa test commands, with different flags, on 26 directories of policies, through both
opa and rego4k. It compares their output and exit codes.
The README of rego4k-testing-core lists the
differences from opa test, for example the flags --coverage and --bench, which rego4k does not have.