【发布时间】:2015-04-13 13:24:53
【问题描述】:
Spock 测试将 null 注入到 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 应用程序充气?
【问题讨论】:
-
这是单元测试所以你不能自动装配。
-
你是如何运行这个测试的?
-
显示你要测试的实际功能。
-
这是一个预备步骤,我在 resources.groovy 中有 bean,我想重复使用并坚持下去
-
我运行测试,通过默认的 intellij 配置进行规范调试