【问题标题】:Strange error message while doing Grails integration testing in IntelliJ在 IntelliJ 中进行 Grails 集成测试时出现奇怪的错误消息
【发布时间】:2015-11-19 10:38:22
【问题描述】:

对于我的项目,我使用:Grails 2.4.4 和 IntelliJ IDEA 14.0.2

我有一个用户域类。我为这个类创建了一个集成测试。这里是:

package com.kunega

import grails.test.spock.IntegrationSpec

class UserIntegrationSpec extends IntegrationSpec {

    def setup() {
        def appAdmin = new User(username: 'appAdmin', enabled: true, password: 'password').save(flush: true)
    }

    def cleanup() {
    }

    void "findUser"() {
        expect: User.findByUsername("appAdmin") != null
    }
}

当我运行“grail test-app -integration”命令时,IntelliJ 的“运行”选项卡中出现以下错误:

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)

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/grails-2.4.4/dist/grails-plugin-log4j-2.4.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/grails-2.4.4/lib/org.slf4j/slf4j-simple/jars/slf4j-simple-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.GrailsSlf4jLoggerFactory]

但是,在“控制台”选项卡中有这条消息,表明测试通过了:

|Running 1 integration test... 1 of 1
|Completed 1 integration test, 0 failed in 0m 0s
.
|Tests PASSED - view reports in D:\Facultate\Sport app\Kunega\target\test-reports

我不知道该怎么想。我应该认为测试通过了还是有问题需要解决?如果您需要更多详细信息,例如 BuildConfig.groovy 等,请告诉我。如果有人可以提供解释,那就太好了。谢谢你。

【问题讨论】:

    标签: grails intellij-idea groovy integration-testing applicationcontext


    【解决方案1】:

    当您对域执行集成测试时,您需要模拟其中的值。我的建议是将 spock 与 Mock mixin 一起使用。您可能还想探索 TestFor mixin。由于这是一个集成测试,您可能还需要模拟诸如 UserRole 和 Role 类之类的东西,或者您正在使用的任何等价物。您的代码应如下所示:

    package com.kunega
    
    import grails.test.mixin.Mock
    import grails.test.mixin.TestFor
    import spock.lang.Specification
    
    /**
     * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin}          for usage instructions
     */
     @TestFor(User)
    
     //The User class is mocked below but you will also want to mock the integrated classes as this is an integration test
     @Mock([User])
     class UserIntegrationSpec extends IntegrationSpec {
    
    void setup() {
        mockDomain(User, [
            [username: 'appAdmin', enabled: true, password: 'password']])
    }
    
    void "test user exists"() {
        //test logic to test that the user is created in domain
    }
    }
    

    【讨论】:

    • 效果一样。我收到以下异常:“java.lang.IllegalStateException: 找不到 ApplicationContext,首先在 grails.test.spock.IntegrationSpec.$spock_initializeSharedFields(IntegrationSpec) 的 grails.util.Holders.getApplicationContext(Holders.java:97) 正确配置 Grails .groovy:41)"
    猜你喜欢
    • 2013-03-23
    • 2010-12-07
    • 2020-06-30
    • 2012-03-22
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多