gpt4 book ai didi

grailsApplication Autowiring 到 spock 测试总是为空

转载 作者:行者123 更新时间:2023-12-02 13:55:52 24 4
gpt4 key购买 nike

Spock 测试将 null 注入(inject)到 grailsApplication,因为我尝试自动连接到 grails 服务和域对象(规范)

代码(AttackSpec.groovy)

package core

import grails.test.mixin.TestFor
import grails.test.mixin.TestMixin
import grails.test.mixin.support.GrailsUnitTestMixin
import spock.lang.Specification

/**
* See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
*/
@TestMixin(GrailsUnitTestMixin)
@TestFor(Attack)
class AttackSpec extends Specification {


def grailsApplication


def setup() {
Attack attack = new Attack();
println 'app '+grailsApplication.toString()
}

def cleanup() {

}

void "test something"() {
setup:
println 'app '+grailsApplication.toString()
}
}

输出
log4j:WARN No appenders could be found for logger (org.codehaus.groovy.grails.commons.DefaultGrailsApplication).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
appnull
appnull
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/ops/grails-2.4.4/dist/grails-plugin-log4j-2.4.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/ops/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]

我尝试过使用显式 @Autowired 注释和静态类型,但它总是一样的。似乎我必须在某处膨胀 grails 应用程序?

最佳答案

听起来您想要运行集成测试而不是单元测试。将您的类移动到集成测试文件夹下,您可能需要消除您的测试类注释,因为我认为这些注释仅用于单元测试。

关于grailsApplication Autowiring 到 spock 测试总是为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29606460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com