Skip to content

Where queries in integration tests not filtering results #11202

Open

Description

@tircnf

Task List

  • Steps to reproduce provided
  • Example that reproduces the problem uploaded to Github
  • Full description of the issue provided (see below)

Steps to Reproduce

  1. Using grails 3.3.8, create a simple domain class. ('Thing' with a single property, name)
  2. Create an integration or unit test that creates two things... "Thing 1" and "Thing 2".
  3. Then search for those things with a where query.
    expect: Thing.where { name == "thing 1" }.list().size() == 1

Expected Behaviour

The test should run a query similar to: "select ... from thing where this_.name=?"
and pass.

Actual Behaviour

The query runs a query similar to "select ... from thing", returns both things, and the test fails.

Environment Information

  • Operating System: Windows
  • **Grails Version: 3.3.8
  • **JDK Version: 1.8
  • Container Version (If Applicable): running tests with gradle test.

Example Application

[email protected]:tircnf/grailsWhereIntegrationTest.git

More information

The same behavior is found in both unit and integration tests.

I wrote some tests that show some weird behavior. For instance,
if you push the where query into a closure, it works.

In the below test, the first expect line will pass, which runs the Where clause in a closure.
The 2nd expect line will fail, which runs the where query directly.

void "test where -- fails, and the sql doesn't filter by name"() {

    given: "two things"
    new Thing(name: "thing 1").save(flush: true, failOnError: true)
    new Thing(name: "thing 2").save(flush: true, failOnError: true)
 
    def queryClosure = { -> Thing.where { name == "thing 1" } }

    expect:
    queryClosure.call().list().size() == 1  // works
    Thing.where { name == "thing 1" }.list().size() == 1   // fails
}

Activity

  1. tircnf commented on Dec 8, 2018

    @tircnf
    Author

    This has been around for a while. The same thing happens with grails 2.3.11

  2. tircnf commented on Dec 8, 2018

    @tircnf
    Author

    This appears to be an issue with the spock testing framework.

    It just doesn't like it if you have a test comparison and a where query.

    expect:
    def result=Thing.where { name == "thing 1" }.list().size()
    result==1  // works.
    Thing.where { eq 'name', 'thing 1' }.list().size() == 1   // works
    Thing.where { name == "thing 1" }.list().size() == 1   // fails
    

    I'm going out on a limb and say it has something to do with the code that generates the diff view when comparisons fail.

    Hibernate: select this_.id as id1_0_0_, this_.version as version2_0_0_, this_.name as name3_0_0_ from thing this_
    
    Condition not satisfied:
    
    Thing.where { name == "thing 1" }.list().size()==1
          |                           |      |     |
          |                           |      2     false
          |                           [wheretest.Thing : 7, wheretest.Thing : 8]
          grails.gorm.DetachedCriteria@4db3b321
    

    whatever magic is used to process that closure doesn't play well with testing.

  3. self-assigned this
    on Apr 17, 2019
  4. jamesfredley commented on Feb 26, 2026

    @jamesfredley
    Contributor

    This was reported against Grails 3.3.8. The underlying issue was tracked separately in grails-data-mapping (ref: https://github.com/grails/grails-data-mapping/issues/1026 as noted in the comments). The where-query AST transformation has received significant fixes in Grails 7.x that address how criteria are built in test contexts.

    If you are still experiencing this issue on Grails 7.x, please reopen with a minimal reproducer against the current version.

  5. jamesfredley commented on Feb 26, 2026

    @jamesfredley
    Contributor

    Follow-up with verification results: After running reproduction tests against the current 7.0.x codebase, this issue is confirmed fixed.

    Where queries with equality filters now correctly generate SQL with the WHERE clause in standard usage (controllers, services, when/then Spock blocks, closures). The fix was part of commit 4299a24bca which set implicitThis=false on delegate MethodCallExpressions in the DetachedCriteriaTransformer.

    Note: There remains a minor edge case where using Domain.where { ... }.list() directly inside a Spock expect: block may still produce empty criteria due to AST transform ordering with Spock's own transforms. This is a Spock interop edge case, not a GORM bug. Use when/then blocks or assign the where query to a variable as a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions