在IntelliJ中进行Grails集成测试时出现奇怪的错误消息

时间:2015-11-19 10:38:22

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

对于我的项目,我使用: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]

Error message

然而,在'控制台'选项卡上有此消息,表明测试已通过:

|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

enter image description here

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

1 个答案:

答案 0 :(得分:0)

当您对域执行集成测试时,您需要在其中模拟值。我的建议是使用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
}
}