Testing¶
Steps are tested with Jenkins Pipeline Unit (JPU) — a JUnit harness that loads and executes shared library steps without a running Jenkins instance.
Running tests¶
./gradlew test # all tests
./gradlew test --tests DetectAgentTest # single class
./gradlew test --tests "DetectAgentTest.detects nodejs via package_json" # single method
./gradlew clean test # clean first
Results: build/reports/tests/test/index.html
How JPU works¶
Each test class extends BasePipelineTest. In setUp(), call super.setUp() to initialise the mock helper, then register mocks for every DSL method the step under test calls:
helper.registerAllowedMethod('fileExists', [String.class], { path -> path == 'package.json' })
helper.registerAllowedMethod('error', [String.class], { String msg -> throw new RuntimeException(msg) })
binding.setVariable('env', [MY_VAR: 'value'])
Load the step with loadScript('vars/stepName.groovy'), then invoke it:
Key constraints¶
Java 25 + dots in method names — Groovy encodes the string literal in void 'test name'() directly as the JVM method name. Java 25 rejects names containing .. Always replace dots with underscores in test method names:
// ✗ fails on Java 25
void 'detects nodejs via package.json'() { ... }
// ✓ correct
void 'detects nodejs via package_json'() { ... }
Re-registering mocks — helper.registerAllowedMethod overwrites the previous registration for the same signature. Register a safe default in setUp(), then override per-test as needed.
Dependencies¶
| Library | Version | Why |
|---|---|---|
com.lesfurets:jenkins-pipeline-unit |
1.30 | JPU test harness — published to repo.jenkins-ci.org, not Maven Central |
org.codehaus.groovy:groovy-all |
3.0.25 | Includes ASM 9.7, required for Java 25 class file support |
org.junit.jupiter:junit-jupiter |
5.10.x | JUnit 5 engine |