Skip to content

Groovy with method bug fix in Spock 2.4-M7 #2269

Closed

Description

@lukasgraef

Describe the bug

I've stumbled over a possible regression in Spock 2.4-M7 when using Groovy's with{}-closure within a then: or expect: block.

The following two Reproducer tests both fail on M7 while both tests are working on M6.

To Reproduce

import spock.lang.Specification

class ReproducerSpec extends Specification {
    def "assert true in closure"() {
        expect:
        "foo".with {
            assert true
        }
    }

    def "return false in closure"() {
        expect:
        "bar".with {
            false
        }
    }
}

Expected behavior

I'm not sure whether the example above is a regression, because I think the behaviour in M7 (asserting the return value of with{}) seems more consistent than the previous behaviour. I just wanted to report this, as it breaks existing test cases, so this new behaviour should at least be clearly documented.

Actual behavior

Java version

Java 17

Buildtool version

Gradle 8.13

What operating system are you using

Windows

Dependencies

org.spockframework:spock-core:2.4-M7-groovy-3.0

Additional context

No response

Activity

  1. lukasgraef commented on Nov 24, 2025

    @lukasgraef
    Author
  2. added
    regression
    on Nov 24, 2025
  3. Vampire commented on Nov 24, 2025

    @Vampire
    Member

    This actually just was a bug-fix.
    Spock behaved wrong, as it confused the GDK with method with our own with method and did then some rewriting, but also just half rewriting.
    If you for example have

    expect:
    assert null.with {
        false
    }

    the test also was successful as the rewriting did remove the assert but did not add our power assertion code, so no assertion was checked here.

    The two cases you listed are like you said, previously handled wrong by Spock,
    as Spock should check the top-level expression as implicit assertion and did not, as for the first the value of the expression is null, for the second false.

    If anything breaks due to that change, it was because the tests relied on this bug which was not a good idea in the first place I'd say.
    This PR by me fixed those cases and some others: #2162

    Not sure whether we should list the fixing of a bug in Spock as breaking change if it only breaks code that relied on a bug. 🤷‍♂️

  4. AndreasTu commented on Nov 24, 2025

    @AndreasTu
    Member

    Not sure whether we should list the fixing of a bug in Spock as breaking change if it only breaks code that relied on a bug.

    I would say we should not list it as breaking change.
    But maybe add a note to the release notes, that this bug was now fixed regarding the with method, and tests relying on the spock bug may break.
    @Vampire what do you think?

  5. added a commit that references this issue on Nov 24, 2025
    0157a4c
  6. changed the title [-]Possible Regression in Spock 2.4-M7[/-] [+]Groovy with method bug fix in Spock 2.4-M7[/+] on Nov 24, 2025
  7. Vampire commented on Nov 24, 2025

    @Vampire
    Member

    I don't have too strong feelings about it.
    But sounds good to me. :-)

  8. added and removed
    regression
    on Nov 24, 2025
  9. AndreasTu commented on Nov 24, 2025

    @AndreasTu
    Member

    I have created the PR #2270 to update the ReleaseNotes.

  10. added a commit that references this issue on Nov 24, 2025
    15ca7e9
  11. Vampire commented on Nov 27, 2025

    @Vampire
    Member

    Just for future readers here too,
    you can mitigate the change by either using the implicit assertion opt-out operator !! or the Spock with like:

    with("foo") {
        true
    }
     
    !!"bar".with {
        false
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions