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

build.gradle.kts
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 GitHub

Write 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:

authz_test.rego
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

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:

PolicyTests.kt
@TestFactory
fun policies() = RegoTests.junit(
    Rego.standard(Re2Engine),
    File("src/main/kotlin/rego4k/examples/policies"),
)
PolicyTests.kt, lines 24 to 28, on GitHub

To run the tests of a bundle, with its manifest, give a TestRunner and the bundle. With traced = true, a failure shows the trace:

PolicyTests.kt
@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 GitHub

Fix 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.

hours.rego
package hours

working_hours if {
	[hour, _, _] := time.clock([time.now_ns(), "Europe/London"])
	hour >= 9
	hour < 18
}

test_working_hours if working_hours
hours.rego, lines 2 to 10, on GitHub
PolicyTests.kt
@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 GitHub

See 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:

sh
just test-rego -v ./policies

A 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.