jamesfredley commented on issue #14351:
URL: https://github.com/apache/grails-core/issues/14351#issuecomment-3936180842

   This is the same root cause as #12602. When `@SpringBean` is used in 
different Spock specifications, Spring creates separate application contexts 
(since mock bean definitions differ). GORM's static enhancer retains a 
reference to the `SessionFactory` from the first context, so when the second 
context starts, GORM tries to use the now-closed `SessionFactory` - resulting 
in "No Session found for current thread."
   
   The solution is to annotate each test class that uses `@SpringBean` with 
`@DirtiesContext`:
   
   ```groovy
   import org.springframework.test.annotation.DirtiesContext
   
   @DirtiesContext
   class VehicleServiceTest extends Specification {
       @SpringBean
       SomeService someService = Mock()
       // ...
   }
   
   @DirtiesContext
   class ManufacturerServiceTest extends Specification {
       @SpringBean
       AnotherService anotherService = Mock()
       // ...
   }
   ```
   
   This tells Spring to discard the context after each spec runs, so the next 
spec gets a fresh context with a properly initialized GORM enhancer. The tests 
will run a bit slower since contexts can't be cached, but each will have a 
valid `SessionFactory` binding.
   
   If test speed is a concern, consider sharing the same `@SpringBean` 
definitions across specs (via a common base class or shared configuration) so 
Spring can reuse a single context.


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to