You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Where queries in integration tests not filtering results #11202
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
}
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.
Activity
tircnf commentedon Dec 8, 2018
This has been around for a while. The same thing happens with grails 2.3.11
tircnf commentedon Dec 8, 2018
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.
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.
whatever magic is used to process that closure doesn't play well with testing.
zyro23 commentedon Dec 11, 2018
ref. https://github.com/grails/grails-data-mapping/issues/1026
jamesfredley commentedon Feb 26, 2026
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.
jamesfredley commentedon Feb 26, 2026
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/thenSpock blocks, closures). The fix was part of commit4299a24bcawhich setimplicitThis=falseon delegate MethodCallExpressions in the DetachedCriteriaTransformer.Note: There remains a minor edge case where using
Domain.where { ... }.list()directly inside a Spockexpect: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. Usewhen/thenblocks or assign the where query to a variable as a workaround.