You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@grails.apache.org by "RAJ-debug858 (via GitHub)" <gi...@apache.org> on 2025/08/21 19:40:45 UTC
[PR] Fix/hibernate session integration tests 14920 [grails-core]
RAJ-debug858 opened a new pull request, #15002:
URL: https://github.com/apache/grails-core/pull/15002
I've successfully implemented a solution to address the Hibernate session and transaction issue in Grails
integration tests. The solution provides a way for integration tests to run with the same transactional context
as the application.
What was implemented:
1. @WithSession annotation
(grails-testing-support-core/src/main/groovy/grails/testing/mixin/integration/WithSession.groovy):
- New annotation that binds a Hibernate session without starting a transaction
- Can be applied at class or method level
- Supports specifying specific datasources
2. Session binding infrastructure:
- GrailsTestSessionInterceptor - Manages session binding without transactions
- WithSessionSpecExtension - Spock extension for handling @WithSession
- WithSessionTransformation - AST transformation for the annotation
3. Enhanced test infrastructure:
- Updated GrailsTestMode to support bindSession flag
- Modified GrailsTestInterceptor to handle session-only binding
- Updated IntegrationSpecConfigurerExtension to enable session binding by default
4. Test examples:
- WithSessionIntegrationSpec - Demonstrates usage of @WithSession
- SessionBindingComparisonSpec - Shows differences between test modes
5. Documentation:
- Added comprehensive documentation in the integration testing guide
How it solves the problem:
The issue described three scenarios:
- Without @Rollback: No session bound → tests fail for operations that work in the app
- With @Rollback: Both session and transaction → tests pass for operations that fail in the app
- With @WithSession: Session without transaction → tests match application behavior
The new @WithSession annotation provides a session binding that matches the OISV (Open Session In View) pattern
used in running applications, where:
- SELECT operations work (session available)
- save() without flush works (no transaction needed)
- save(flush: true) fails without @Transactional (matches runtime behavior)
This ensures integration tests accurately reflect the application's runtime behavior, preventing false positives
and false negatives in test results.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix/hibernate session integration tests 14920 [grails-core]
Posted by "jdaugherty (via GitHub)" <gi...@apache.org>.
jdaugherty commented on code in PR #15002:
URL: https://github.com/apache/grails-core/pull/15002#discussion_r2293943614
##########
grails-testing-support-core/src/main/groovy/org/grails/testing/context/junit4/GrailsTestConfiguration.java:
##########
@@ -29,7 +29,7 @@
* @author Graeme Rocher
* @since 3.0
*/
-@ContextConfiguration(loader = GrailsApplicationContextLoader.class)
+@ContextConfiguration(loader = SpringBootContextLoader.class)
Review Comment:
Why aren't we using the Grails Context?
##########
grails-testing-support-core/build.gradle:
##########
@@ -48,6 +48,7 @@ dependencies {
api project(':grails-databinding')
api project(':grails-datamapping-core')
api project(':grails-test-core')
+ api('org.spockframework:spock-core') { transitive = false }
Review Comment:
this is incudes by test-core so it should be unnecessary
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix/hibernate session integration tests 14920 [grails-core]
Posted by "borinquenkid (via GitHub)" <gi...@apache.org>.
borinquenkid commented on PR #15002:
URL: https://github.com/apache/grails-core/pull/15002#issuecomment-5007573633
@rgc-code If you want this kept open update it with the changes requested
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix/hibernate session integration tests 14920 [grails-core]
Posted by "RAJ-debug858 (via GitHub)" <gi...@apache.org>.
RAJ-debug858 commented on PR #15002:
URL: https://github.com/apache/grails-core/pull/15002#issuecomment-3212891365
Thank you! I’ve rebased my changes onto the 7.0.x branch and force-pushed the updated branch. The PR now targets 7.0.x as requested.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix/hibernate session integration tests 14920 [grails-core]
Posted by "jdaugherty (via GitHub)" <gi...@apache.org>.
jdaugherty commented on code in PR #15002:
URL: https://github.com/apache/grails-core/pull/15002#discussion_r2294199663
##########
grails-testing-support-core/src/main/groovy/org/grails/test/support/GrailsTestSessionInterceptor.groovy:
##########
@@ -0,0 +1,187 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.grails.test.support
+
+import grails.testing.mixin.integration.WithSession
+import org.grails.datastore.mapping.core.connections.ConnectionSource
+import org.hibernate.FlushMode
+import org.hibernate.Session
+import org.hibernate.SessionFactory
+import org.springframework.context.ApplicationContext
+import org.springframework.orm.hibernate5.SessionHolder
+import org.springframework.transaction.support.TransactionSynchronizationManager
+import groovy.transform.CompileStatic
+import org.slf4j.Logger
+import org.slf4j.LoggerFactory
+
+/**
+ * Binds Hibernate sessions to the test thread without starting transactions.
+ * This mimics the OISV (Open Session In View) pattern used in running applications.
+ */
+@CompileStatic
+class GrailsTestSessionInterceptor {
+
+ private static final Logger LOG = LoggerFactory.getLogger(GrailsTestSessionInterceptor)
Review Comment:
This is a groovy class, is there a reason you can't use `@Slf4j` ?
##########
grails-test-core/src/main/groovy/org/grails/test/support/GrailsTestInterceptor.groovy:
##########
@@ -30,6 +30,7 @@ class GrailsTestInterceptor {
private String[] testClassSuffixes
private GrailsTestTransactionInterceptor transactionInterceptor
+ private GrailsTestSessionInterceptor sessionInterceptor
Review Comment:
This is not compiling.
##########
grails-testing-support-core/src/main/resources/META-INF/services/org.spockframework.runtime.extension.IGlobalExtension:
##########
@@ -1 +1 @@
-org.grails.testing.spock.TestingSupportExtension
+org.grails.test.spock.WithSessionSpecExtension
Review Comment:
What about the original extension? Why are we replacing the old behavior instead of augmenting it?
##########
grails-test-core/src/main/groovy/org/grails/test/spock/IntegrationSpecConfigurerExtension.groovy:
##########
@@ -60,7 +60,10 @@ class IntegrationSpecConfigurerExtension implements IAnnotationDrivenExtension<A
IntegrationSpecMethodInterceptor(ApplicationContext applicationContext) {
this.applicationContext = applicationContext
- this.mode = new GrailsTestMode(autowire: true, wrapInTransaction: true, wrapInRequestEnvironment: true)
+ // By default, bind session for integration tests to match application behavior
Review Comment:
The application behavior depends if OSIV is configured and if hibernate is used. We should have our tests default to best behaviors, or even better, detect the configuration and then only apply it based on the application configuration.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix/hibernate session integration tests 14920 [grails-core]
Posted by "jamesfredley (via GitHub)" <gi...@apache.org>.
jamesfredley commented on PR #15002:
URL: https://github.com/apache/grails-core/pull/15002#issuecomment-3329264254
This needs refactored to be an optional feature vs replacing the existing feature.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix/hibernate session integration tests 14920 [grails-core]
Posted by "jamesfredley (via GitHub)" <gi...@apache.org>.
jamesfredley commented on PR #15002:
URL: https://github.com/apache/grails-core/pull/15002#issuecomment-3212047564
@RAJ-debug858 Thank you. Can you resubmit with the a target branch of 7.0.x?
When I try to make this change I get "There are no new commits between base branch '7.0.x' and head branch 'fix/hibernate-session-integration-tests-14920'", so I am guessing some of your new commits are not present on your forked branch.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix/hibernate session integration tests 14920 [grails-core]
Posted by "rgc-code (via GitHub)" <gi...@apache.org>.
rgc-code closed pull request #15002: Fix/hibernate session integration tests 14920
URL: https://github.com/apache/grails-core/pull/15002
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix/hibernate session integration tests 14920 [grails-core]
Posted by "RAJ-debug858 (via GitHub)" <gi...@apache.org>.
RAJ-debug858 commented on PR #15002:
URL: https://github.com/apache/grails-core/pull/15002#issuecomment-3214593994
- grails-test-core/build.gradle (modified)
- grails-testing-support-core/build.gradle (modified)
- The moved files in grails-testing-support-core/src/main/groovy/org/grails/test/
- grails-testing-support-core/src/main/resources/META-INF/services/org.spockframework.runtime.extension.IGlobalE
xtension (modified)
- The fixed Java file with the import change
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix/hibernate session integration tests 14920 [grails-core]
Posted by "jdaugherty (via GitHub)" <gi...@apache.org>.
jdaugherty commented on code in PR #15002:
URL: https://github.com/apache/grails-core/pull/15002#discussion_r2293908024
##########
grails-testing-support-core/build.gradle:
##########
@@ -47,7 +47,8 @@ dependencies {
api project(':grails-databinding')
api project(':grails-datamapping-core')
- api project(':grails-test-core')
Review Comment:
test-core is meant to be the parent to this dependency. Why are you removing it?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix/hibernate session integration tests 14920 [grails-core]
Posted by "jdaugherty (via GitHub)" <gi...@apache.org>.
jdaugherty commented on PR #15002:
URL: https://github.com/apache/grails-core/pull/15002#issuecomment-3214647514
Can't the test behavior be driven by whether the application uses OSIV?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix/hibernate session integration tests 14920 [grails-core]
Posted by "RAJ-debug858 (via GitHub)" <gi...@apache.org>.
RAJ-debug858 commented on code in PR #15002:
URL: https://github.com/apache/grails-core/pull/15002#discussion_r2293965145
##########
grails-testing-support-core/src/main/groovy/org/grails/testing/context/junit4/GrailsTestConfiguration.java:
##########
@@ -29,7 +29,7 @@
* @author Graeme Rocher
* @since 3.0
*/
-@ContextConfiguration(loader = GrailsApplicationContextLoader.class)
+@ContextConfiguration(loader = SpringBootContextLoader.class)
Review Comment:
okay we should use the Grails-specific context loader since it provides Grails
application context setup rather than just generic Spring Boot context. The GrailsApplicationContextLoader
extends SpringBootContextLoader but adds Grails-specific initialization through GrailsApp.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix/hibernate session integration tests 14920 [grails-core]
Posted by "RAJ-debug858 (via GitHub)" <gi...@apache.org>.
RAJ-debug858 commented on code in PR #15002:
URL: https://github.com/apache/grails-core/pull/15002#discussion_r2293950472
##########
grails-testing-support-core/build.gradle:
##########
@@ -47,7 +47,8 @@ dependencies {
api project(':grails-databinding')
api project(':grails-datamapping-core')
- api project(':grails-test-core')
Review Comment:
Restored grails-test-core dependency in grails-testing-support-core
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix/hibernate session integration tests 14920 [grails-core]
Posted by "jdaugherty (via GitHub)" <gi...@apache.org>.
jdaugherty commented on code in PR #15002:
URL: https://github.com/apache/grails-core/pull/15002#discussion_r2293909227
##########
grails-test-core/build.gradle:
##########
@@ -31,6 +31,7 @@ dependencies {
implementation platform(project(':grails-bom'))
api 'org.springframework:spring-tx'
+
Review Comment:
The extra newline should be removed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix/hibernate session integration tests 14920 [grails-core]
Posted by "jdaugherty (via GitHub)" <gi...@apache.org>.
jdaugherty commented on PR #15002:
URL: https://github.com/apache/grails-core/pull/15002#issuecomment-3212879044
FYI: you can add your branch as a remote, fetch, cherry-pick your change, and then adjust for where its supposed to be. GitHub is showing 10,000+ commits so my guess is this change was made against an earlier branch before we created the mono repo.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org