JUnit Jupiter
This guide shows how to use ReqCover with JUnit Jupiter tests, from dependencies to parameterized tests.
Add the dependencies
Section titled “Add the dependencies”Gradle (Kotlin DSL), with the GitHub Packages repository from Installation:
dependencies { testImplementation("dev.reqcover:reqcover-engine-junit-jupiter:0.0.12") testImplementation("dev.reqcover:reqcover-source-openapi:0.0.12") testImplementation("dev.reqcover:reqcover-reporter-html:0.0.12") testImplementation("dev.reqcover:reqcover-reporter-xml:0.0.12")}
tasks.test { useJUnitPlatform()}Maven uses reqcover-engine-junit-jupiter-jvm and the other -jvm artifacts; see the Maven guide.
Registration
Section titled “Registration”The engine contains a TestExecutionListener (dev.reqcover.junit.jupiter.RequirementsCoverageListener) registered through META-INF/services.
The JUnit Platform loads it with the ServiceLoader as soon as the module is on the test classpath. No annotation, extension or build plugin is needed.
Configure in junit-platform.properties
Section titled “Configure in junit-platform.properties”Create src/test/resources/junit-platform.properties:
reqCover.requirementsUris=requirements.yamlreqCover.minimumRequiredCoveragePercent=100reqCover.failIfBelowMinimum=truereqCover.reportsDir=build/reports/tests/reqcoverreqCover.requirementsUris takes a comma-separated list of classpath locations, for example users.yaml, orders.yaml.
Any other way of setting JUnit configuration parameters (launcher, build tool) works too. All keys are in Configuration.
Override with system properties
Section titled “Override with system properties”JVM system properties take precedence over JUnit configuration parameters. Use them to change settings per build without editing files.
Gradle:
tasks.test { useJUnitPlatform() systemProperty("reqCover.minimumRequiredCoveragePercent", "80")}Maven Surefire:
<systemPropertyVariables> <reqCover.minimumRequiredCoveragePercent>80</reqCover.minimumRequiredCoveragePercent></systemPropertyVariables>Annotate tests
Section titled “Annotate tests”Java needs the named argument id:
import dev.reqcover.api.ForRequirement;
@Test@ForRequirement(id = "#getUsers/200")@ForRequirement(id = "#getUsers/200/sortedByName")void listsUsersSortedByName() { }Kotlin can use the short form:
import dev.reqcover.api.ForRequirement
@Test@ForRequirement("#getUsers/200")@ForRequirement("#getUsers/200/sortedByName")fun `lists users sorted by name`() { }A passing test covers every requirement it is annotated with.
Annotate a test class
Section titled “Annotate a test class”Since 0.0.12.
Put @ForRequirement on the class when several tests together verify one requirement. The requirement is covered when at least one test in the class passes.
@ForRequirement(id = "#getUsers/200")class ListUsersTest { @Test void returnsAllUsers() { } @Test void returnsEmptyList() { }}@ForRequirement("#getUsers/200")class ListUsersTest { @Test fun `returns all users`() { } @Test fun `returns empty list`() { }}Class-level and method-level annotations add up: a passing test covers the requirements of its method and of its class.
Parameterized, repeated and dynamic tests
Section titled “Parameterized, repeated and dynamic tests”For @ParameterizedTest, @RepeatedTest and the dynamic tests of a @TestFactory, the requirement is covered if at least one invocation passes. Failing, skipped and aborted invocations do not count.
@ParameterizedTest@ValueSource(strings = {"alice", "bob"})@ForRequirement(id = "#getUser/200")void findsUser(String name) { }The removed reqCover.requirementsUri setting
Section titled “The removed reqCover.requirementsUri setting”The singular reqCover.requirementsUri was removed in 0.0.10. If it is still set, as a system property or configuration parameter, the run stops with:
The reqCover.requirementsUri configuration parameter has been removed. Use reqCover.requirementsUris instead.Rename the key to reqCover.requirementsUris. See Migration.