3

I am using Grails 3.3.1 and I am getting a "No Session found for current thread" error when running all of my integration tests using the Gradle "integrationTest" command.

I have several service integrations tests which run fine on their own. Similarly, I have GEB tests which run with no failed tests. However, when I include the running of GEB tests with the service tests, any service tests which occur after the GEB tests fail with "org.hibernate.HibernateException: No Session found for current thread". The service tests which run before the GEB tests pass without issue.

Below is a sample of the service integration tests. The error is thrown when the first object is attempted to be saved to the database.

@Rollback
@Integration
class AppUserServiceSpec extends Specification {

    //--uses Test database
    @Autowired AppUserService appUserService
    SessionFactory sessionFactory


    private AppUser setupData(){
        //--data rolled back after each test
        AppUser appUser = new AppUser(username: 'a', firstName: 'a', lastName: 'a', enabled: true).save(flush: true, failOnError: true)
        new AppUser(username: 'b', firstName: 'b', lastName: 'b', enabled: true).save(flush: true, failOnError: true)
        new AppUser(username: 'c', firstName: 'c', lastName: 'c', enabled: true).save(flush: true, failOnError: true)
        new AppUser(username: 'd', firstName: 'd', lastName: 'd', enabled: true).save(flush: true, failOnError: true)
        new AppUser(username: 'e', firstName: 'e', lastName: 'e', enabled: true).save(flush: true, failOnError: true)

        return appUser
    }


    void "Test something"() {
        given:
        setupData()

        //--test stuff here
    }

I suspect it is relation to the application being started up twice by the tests: once for the first regular integration test and again for the first GEB test.

Testing started at 3:48 PM ...
3:48:24 PM: Executing task 'integrationTest --tests *.*'...

:compileJava NO-SOURCE
:compileGroovy
:buildProperties
:processResources
:classes
:configureChromeDriverBinary SKIPPED
:configureGeckoDriverBinary SKIPPED
:compileTestJava NO-SOURCE
:compileTestGroovy
:processTestResources NO-SOURCE
:testClasses
:compileIntegrationTestJava NO-SOURCE
:compileIntegrationTestGroovy
:processIntegrationTestResources
:integrationTestClasses
:integrationTest
2019-01-07 15:48:43.048  INFO My Application 2019-01-07 15:48:43.048  INFO My Application 
Configuring Spring Security Core ...
... finished configuring Spring Security Core


Configuring Spring Security LDAP ...
... finished configuring Spring Security LDAP

2019-01-07 15:48:54.065  INFO My Application Grails application running at http://localhost:51962/application in environment: test
2019-01-07 15:48:54.706  INFO My Application 2019-01-07 15:48:54.706  INFO My Application 
Configuring Spring Security Core ...
... finished configuring Spring Security Core


Configuring Spring Security LDAP ...
... finished configuring Spring Security LDAP

2019-01-07 15:48:57.847  INFO My Application Grails application running at http://localhost:51969/application in environment: test
Started InternetExplorerDriver server (32-bit)
2.47.0.0
Listening on port 36464

No Session found for current thread
org.hibernate.HibernateException: No Session found for current thread
Tabbykat
  • 41
  • 2

1 Answers1

0

Try getting the current session, which should also create a new one if one doesnt exist:

private AppUser setupData(){
        def currentSession = SessionFactory.getCurrentSession()
                AppUser appUser = new AppUser(username: 'a', firstName: 'a', lastName: 'a', enabled: true).save(flush: true, failOnError: true)
                new AppUser(username: 'b', firstName: 'b', lastName: 'b', enabled: true).save(flush: true, failOnError: true)
                new AppUser(username: 'c', firstName: 'c', lastName: 'c', enabled: true).save(flush: true, failOnError: true)
                new AppUser(username: 'd', firstName: 'd', lastName: 'd', enabled: true).save(flush: true, failOnError: true)
                new AppUser(username: 'e', firstName: 'e', lastName: 'e', enabled: true).save(flush: true, failOnError: true)
        currentSession.flush()
        return appUser
}