Grails IntegrationSpec IllegalStateException

标签 grails integration-testing spock grails-2.4

从 2.4.0 升级到 2.4.2 后,我在运行集成测试时遇到错误。它表明测试通过了,但是我收到了 IllegalStateException。

    Failure:  |
massemailsystem.UserInformationIntegrationSpec
 |
java.lang.IllegalStateException: Could not find ApplicationContext, configure Grails correctly first
    at grails.util.Holders.getApplicationContext(Holders.java:97)
    at grails.test.spock.IntegrationSpec.$spock_initializeSharedFields(IntegrationSpec.groovy:41)

我尝试分析测试,但没有发现任何异常情况。这是完整的测试。我正在测试从 LDAP 数据源获取信息

abstract class DirectoryIntegrationSpec extends IntegrationSpec {

    DirContextOperations getContext(Map attrs) {
        DirContextAdapter d = new DirContextAdapter()
        attrs.each { k, v ->
            if (v != null) {
                d.addAttributeValue(k, v)
            }
        }
        d
    }
}


class UserInformationIntegrationSpec extends DirectoryIntegrationSpec {

    def dataSource

    def setup() {
        new SPRIDEN(pidm: 100, yNumber: 'Y00100', lastName: 'Smith').save(flush: true, failOnError: true)
        new SPBPERS(pidm: 100, prefFirstName: 'Joe', activityDate: new Date(), armedServMedVetInd: 'N').save(flush: true, failOnError: true)
        new SPRTELE(pidm: 100, seqNo: 1, teleCode: 'CE', activityDate: new Date(), primaryInd: 'Y', phoneArea: '330', phoneNumber: '1234567').save(flush: true, failOnError: true)

        new SPRIDEN(pidm: 102, yNumber: 'Y00102', lastName: 'Smith').save(flush: true, failOnError: true)
        new SPRTELE(pidm: 102, seqNo: 1, teleCode: 'CE', activityDate: new Date(), primaryInd: 'Y', phoneArea: '330', phoneNumber: '1234567').save(flush: true, failOnError: true)

        new SPRIDEN(pidm: 103, yNumber: 'Y00103', lastName: 'Smith').save(flush: true, failOnError: true)
    }

    def cleanup() {
    }

    @Unroll
    void "test constructor from LDAP for fake #employeeid"() {
        when:
        def context = getContext([employeeid: employeeid, givenname: firstName, sn: lastName, mail: email])
        UserInformation u = new UserInformation(context, username, dataSource)

        then:
        u.id == id
        u.firstName == prefFirstName
        u.lastName == lastName
        u.email == email
        u.phone == phone
        u.department == department
        u.username == username

        where:
        employeeid | username | id   | firstName | prefFirstName | lastName | email            | phone            | department
        'Y00100'   | 'jsmith' | 100  | 'Joseph'  | 'Joe'         | 'Smith'  | '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d372e303429351d242e2873383928" rel="noreferrer noopener nofollow">[email protected]</a>' | '(330) 123-4567' | null
        "Y00101"   | null     | null | null      | null          | null     | null             | null             | null
        "Y00102"   | 'jsmith' | 102  | 'Joseph'  | 'Joseph'      | 'Smith'  | '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="68021b05011c0028111b1d460d0c1d" rel="noreferrer noopener nofollow">[email protected]</a>' | '(330) 123-4567' | null
        "Y00103"   | 'jsmith' | 103  | 'Joseph'  | 'Joseph'      | 'Smith'  | null             | null             | null
    }
}

提前致谢!

编辑:当我单独运行测试时,测试不会失败,只有当我运行所有集成测试时。

最佳答案

我这边有一个愚蠢的问题。

测试之前的测试使用 @TestFor 注释并扩展 Specification,而它应该扩展 IntegrationSpec 而没有根本没有注释。我将此规范从一个单元更改为一个集成,但我未能更改这些内容。

有趣的是测试本身运行良好,只有在出现问题后才运行测试。

关于Grails IntegrationSpec IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24957901/

相关文章:

validation - Grials 更新因唯一约束而失败

spring - 当检查调用时,模拟方法也返回 null

maven - 在Heroku中的Grails应用上制作新的文物

javascript - 集成组件中的 Emberjs 链接到 href 测试

oauth-2.0 - 谷歌是否提供测试用户进行集成测试

java - 与Web交互的应用程序的集成测试

gradle - 在Gradle中测试目录是否存在?

grails - 如何在Grails单元测试中模拟Ajax请求

Grails 2.5.3 使用 spock 在集成测试中测试服务回滚

Grails Spring Security SAML 插件 "SPSSODescriptor wasn' t 找到”