Skip to content

Script under test doesn't correctly handle field annotated @Field in script's methods  #120

@PavelKa

Description

@PavelKa

Expected Behavior

The tested script correctly handles fields annotated @field in all methods

Actual Behavior

Methods access the wrong instance of the field

Steps to Reproduce

define script :
vars/testFiled.groovy

import groovy.transform.Field
@Field f = "test"
def call(Map _args) {
    node {
        stage("set f") {
            echo(f)
            f = "test2"
            setF()
        }
    }
}
def setF() {
   // field f should contains value "test2" but contains "test" instead
    echo (f)
}

define test:

import com.homeaway.devtools.jenkins.testing.JenkinsPipelineSpecification

class TestFieldSpec extends JenkinsPipelineSpecification {
    def "should echo test test2"() {
        given:
        def testField = loadPipelineScriptForTest("vars/testField.groovy")
        when:
        testField()
        then:
        1 * getPipelineMock("echo")('test')
        1 * getPipelineMock("echo")('test2')
    }
}

Test fails:
Too many invocations for:

1 * getPipelineMock("echo")('test') (2 invocations)

Additional Information

Tested with Java 1.8 and 11 and jenkins-spock.version 2.1.5

Script works as expected in Jenkins

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions