Reputation: 1
What I'm trying to do is create a method mock that returns a mock so I can test the folloging disconnectNode method of a class. What I'm trying to mock is the getNode and toComputer calls in the getJenkinsNode method.
Computer getJenkinsNode(String nodeName) {
return Jenkins.getInstanceOrNull().getNode(nodeName).toComputer()
}
def disconnectNode(String nodeName) {
def offlineCause = 'Maintenance'
def node = getNode(nodeName)
node.setTemporarilyOffline(true, new OfflineCause.ByCLI(offlineCause))
}
But, I'm not there yet. I'm trying the figure out why I can't make a mock return another mock.
With jenkins-spock and Spock I managed to do the following test specification just for trying to locate the problem
import com.homeaway.devtools.jenkins.testing.JenkinsPipelineSpecification
import hudson.model.Computer
import hudson.model.Node
import jenkins.model.Jenkins
class MaintenanceSpec extends JenkinsPipelineSpecification {
def 'Script does not fail or throw exceptions'() {
given:
Computer computer = Mock()
Node node = Mock()
node.toComputer() >> computer
Jenkins jenkins = getPipelineMock('Jenkins')
jenkins.getNode('Node 1') >> node
when:
System.out.println(jenkins.getNode('Node 1').toString())
System.out.println(computer.toString())
System.out.println(jenkins.getNode('Node 1').toComputer().toString())
System.out.println(node.toComputer().toString())
then:
noExceptionThrown()
}
}
The printed messages after the test are.
Mock for type 'Node' named 'node'
Mock for type 'Computer' named 'computer'
null
null
The result is really confusing. With the first print I can tell that the getNode function is returning the mocked Node as expected. With the second print I can tell that the mock for the Computer class is created. But the 3rd and 4th prints are returning null so the problem is with the mock for the method toComputer but I don't know what I'm doing wrong
Any ideas?
Thank you!
Upvotes: 0
Views: 569