包含GEB测试时,Grails 3集成测试“当前线程未找到会话”

时间:2019-01-07 22:05:27

标签: grails integration-testing geb

我正在使用Grails 3.3.1,使用Gradle“ integrationTest”命令运行所有集成测试时,出现“找不到当前线程的会话”错误。

我有几个服务集成测试,它们自己运行良好。同样,我有运行GEB测试且没有失败的测试。但是,当我将运行GEB测试与服务测试包括在一起时,GEB测试之后发生的任何服务测试都将失败,并显示“ org.hibernate.HibernateException:未为当前线程找到会话”。在GEB测试通过之前运行的服务测试没有问题。

下面是服务集成测试的示例。尝试将第一个对象保存到数据库时抛出该错误。

@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
    }

我怀疑这与应用程序通过两次测试启动有关:一次是针对首次常规集成测试,另一次是针对首次GEB测试。

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

1 个答案:

答案 0 :(得分:0)

尝试获取当前会话,如果当前会话不存在,它还将创建一个新会话:

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
}