'How do I mock the `message` variable with out refrencing the mock in the function via Java Spock?
The bellow code works. But referencing the mock in the function smells wrong. (but my scent of code-smell could be wrong as well.).
Is there a way to call the mocked variable without referencing it in the function?
Function.
#!/usr/bin/env groovy
def call(String commitId) {
String message = sh(
script: "git show -s --format=%s ${commitId}",
returnStdout: true
)
if (message == null) message = messageMock
echo "Based on the commit message: \n ${message.trim()}"
switch ("${message.trim()}") {
case ~/^(?i)(.*Feature.*)/:
verType = 'minor'
echo "The Next version will be a ${verType} type"
break
case ~/^(?i)(.*(Hot|Bug))fix.*$/:
verType = 'patch'
echo "The Next version will be a ${verType} type"
break
case ~/^.*Release.*$/:
verType = 'major'
echo "The Next version will be a ${verType} type"
break
default:
verType = 'cosmetic'
echo 'cosmetic changes only no new version'
break
}
return verType
}
The unit test via Spock
package com.foo.jenkins
import com.foo.jenkins.testing.JenkinsPipelineSpecification
class SemVerSetTypeSpec extends JenkinsPipelineSpecification {
def "expected minor value"() {
setup:
def SemVerSetType = loadPipelineScriptForTest("vars/semVerSetType.groovy")
SemVerSetType.getBinding().setVariable("messageMock", "Feature Test")
when:
def verType = SemVerSetType("Feature")
then:
1 * getPipelineMock("sh")([script: "git show -s --format=%s Feature", 'returnStdout': true])
expect:
"minor" == verType;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
