I have a method to check if a Jenkins job's next build exists or not within 10 attempts.
void checkNextBuild10Attempts() {
int attempt = 0
while (attempt < 10) {
Job job = JenkinsUtils.getJob(jobName)
Run nextBuild = job.getBuildByNumber(nextBuildNumber)
if (nextBuild) {
break
}
attempt++
}
if (attempt == 10) {
error
}
}
I'm trying to write a unit test for it using PipelineSpockTestBase
. My unit test in short:
given:
JenkinsUtils spy = GroovySpy(JenkinsUtils, constructorArgs: [script], global: true) {
getJob(_) >> new Job(null, 'foo') {
boolean isBuildable() {
return false
}
protected SortedMap _getRuns() {
return null
}
protected void removeRun(Run run) {}
Run getBuildByNumber(int buildNumber) {
return null
}
}
getNextBuildNumber(_) >> 100
}
releaseHelper.jenkinsUtils = spy
where:
releaseHelper.checkNextBuild10Attempts()
How I can return a dummy Run
object by the method getBuildByNumber
to break the while
loop?
Thank you!