I hope you're still reading here. I still have a problem of understanding how the steps are mocked and used in your example. I've managed to rework the testing to move to spock and get rid of all that java stuff.
Now the following. I created a ProjectVersionGetter class to encapsulate the fetching of the current version (which in turn is done by calling a gradle task in the project). For simplification and testing I return a 3.0.0.0 hardly using the following class:
So, I provide a configuration Map where the version should be kept. In my declarative pipeline, I've got a stage like this:
stage("Fetch version"){steps{ex_getversionconfigscript{currentBuild.displayName="[${currentBuild.id} ${config.projectVersion ?: 'No version set!'}"}}}
My simple spock test shall verify, that the projectVersion is set to the configuration map and - for the matter of simplicity - it should be valued to "3.0.0.0".
defsetup(){_context=mock(IContext.class)_steps=mock(IStepExecutor.class)when(_context.getStepExecutor()).thenReturn(_steps)ServiceLocator.registerContext(_context)}def"Version is returned in configuration"(){when:defconfiguration=Defaults.loadDefaults([:])and:"ensure mandatory keys are set"Defaults.getMandatoryKeys().each{configuration.put(it,"dummy_value")}and:defprojectVersionGetter=newProjectVersionGetter(configuration)projectVersionGetter.exec()then:// verify(_steps).sh(anyString())configuration.containsKey('projectVersion')// FIXME: && configuration.get('projectVersion') == '3.0.0.0'}
The key is set, but to null. So, the main problem I have is with the line
Stringversion=steps.sh(true,"echo \"3.0.0.0\"")
So, I added to IStepExecutor and the Implementation a function to cover the return of the stdout to a String, so I can retrieve the version from the "gradle call" (in future):
Hi Adrian,
I hope you're still reading here. I still have a problem of understanding how the steps are mocked and used in your example. I've managed to rework the testing to move to spock and get rid of all that java stuff.
Now the following. I created a ProjectVersionGetter class to encapsulate the fetching of the current version (which in turn is done by calling a gradle task in the project). For simplification and testing I return a 3.0.0.0 hardly using the following class:
The vars file looks like this:
So, I provide a configuration Map where the version should be kept. In my declarative pipeline, I've got a stage like this:
My simple spock test shall verify, that the
projectVersion
is set to the configuration map and - for the matter of simplicity - it should be valued to "3.0.0.0".The key is set, but to
null
. So, the main problem I have is with the lineSo, I added to IStepExecutor and the Implementation a function to cover the return of the stdout to a String, so I can retrieve the version from the "gradle call" (in future):
The implementation is
Can you, or someone point me to the mistake I made?
Thanks a lot,
ferdy