Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No Session found for current thread in spring boot test #656

Open
ryang-bgl opened this issue Jan 25, 2023 · 2 comments
Open

No Session found for current thread in spring boot test #656

ryang-bgl opened this issue Jan 25, 2023 · 2 comments

Comments

@ryang-bgl
Copy link

When using @SpringBean in different SpringBootTest, spring will create a separate context for each test since spring doesn't know if those two tests share the same mock beans. This caused "No Session found for current thread" when running two tests together.

Please clone the following repo to reproduce it.
[email protected]:ruiyang/gorm-spring-boot-hibernate-session-error.git
or
https://github.com/ruiyang/gorm-spring-boot-hibernate-session-error

There are two tests:
VehicleServiceTest
ManufacturerServiceTest

test passes when running each individually

./gradlew test --tests *ManufacturerServiceTest*
./gradlew test --tests *VehicleServiceTest*

test will fail when running together

./gradlew test

Please help!
Thanks

@jrechet
Copy link
Contributor

jrechet commented Mar 4, 2023

Hi,
By default, Spring shares the context across tests.

The @SpringBean annotation from Spock, as i understand it reading the documentation, modify the Spring Context, "preventing it from being reused by Spring’s outside of the current Specification".

To force the reload of the application context, you can use @org.springframework.test.annotation.DirtiesContext annotation to indicate that the context has been modified
hope this helps

@ryang-bgl
Copy link
Author

@jrechet Hi, thanks for looking into this. You are right and the DirtiesContext is what I'm using to workaround the issue.
But I feel it is a workaround rather than a proper solution. It is fine for a project without many tests. If there are many tests, it will make the test run much slower.
Could we investigate why restarting the spring context will cause this issue and if there is a better solution? Last time when I debugged it, I felt it was caused by adding those dynamic findBy or other dynamic methods to the GormEntity.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants