Skip to content

NPE when variable is initialized using a method with the same name in features calling "thrown()" #1266

@rieske

Description

@rieske

Issue description

A variable initialized using a method with the same name, as in def foo = foo() throws a NullPointerException when used in a feature method that calls thrown() in then: block.

How to reproduce

class SomeSpec extends spock.lang.Specification {
    def "foo test"() { // fails
        when:
        def foo = foo() // fails here
        println(foo)
        throw new IllegalStateException()

        then:
        thrown IllegalStateException
    }

    def "bar test"() { // passes
        when:
        def bar = foo()
        println(bar)
        throw new IllegalStateException()

        then:
        thrown IllegalStateException
    }

    String foo() {
        return "foo"
    }
}

foo test fails when running this test class (only tried with Gradle):

Expected exception of type 'java.lang.IllegalStateException', but got 'java.lang.NullPointerException'
	at org.spockframework.lang.SpecInternals.checkExceptionThrown(SpecInternals.java:81)
	at org.spockframework.lang.SpecInternals.thrownImpl(SpecInternals.java:68)
	at SomeSpec.foo test(SomeSpec.groovy:9)
Caused by: java.lang.NullPointerException: Cannot invoke method call() on null object
	at SomeSpec.foo test(SomeSpec.groovy:4)

Additional Environment information

Java/JDK

java -version

openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.8+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.8+10, mixed mode)

Build-tool dependencies used

Gradle 6.8.1

Fails with: testImplementation("org.spockframework:spock-core:2.0-M4-groovy-2.5") and testImplementation("org.spockframework:spock-core:2.0-M4-groovy-3.0")

Works with: testImplementation("org.spockframework:spock-core:1.3-groovy-2.5")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions