简体   繁体   English

grails应用程序自动装配到spock测试始终为null

[英]grailsApplication autowiring to spock tests are always null

Spock tests injects null to grailsApplication,as i tried autowiring both to grails service and to domain objects(spec) Spock测试将空值注入grailsApplication中,因为我尝试自动装配grails服务和域对象(规范)

code(AttackSpec.groovy) 代码(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()
    }
}

output 产量

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]

I have tried using explicit @Autowired annotation and Static Typing,yet it's always the same. 我尝试使用显式@Autowired批注和静态键入,但始终相同。 it seems like I have to inflate the grails application somewhere? 好像我必须在某个地方给grails应用程序充气?

It sounds like you want to be running an integration test and not a unit test. 听起来您想运行集成测试而不是单元测试。 Move your class under the integrations test folder and you may need to eliminate your test class annotations because I think those are only for unit testing. 将您的课程移到集成测试文件夹下,您可能需要删除测试课程注释,因为我认为这些注释仅用于单元测试。

What is the point of injecting grailsApplication in a unit test?. 在单元测试中注入grailsApplication有什么意义? If you want to set a config, because your class under test: Attack reads a value of the grails application, then you can mock the grailsApplication as following: 如果要设置配置,因为您的被测类: Attack读取grails应用程序的值,则可以模拟grailsApplication,如下所示:

def 'test whatever you want to'() {
  def attack = new Attack()
  attack.grailsApplication = [config: [my: [setting: "myValue"]]] // if attack uses grailsApplication.config.my.setting internally

  when: 
  def setting = attack.getMySetting()

  then:
  setting == "myValue"
}

Or you could use a real Mock if you want to track access to it. 或者,如果您想跟踪对它的访问,则可以使用真实的Mock。

If you really want to use the grails environment to test, that access of the values on the framework (for whatever reason that might be) is correct executed, you should use an integration test. 如果您确实想使用grails环境进行测试,那么是否正确执行了对框架值的访问(无论出于何种原因),则应该使用集成测试。 Subclassing IntegrationSpec from Spock will inject the grailsApplication into your test, so you can set the real thing in your Attack class. 从Spock继承IntegrationSpec的子类会将grailsApplication注入到测试中,因此您可以在Attack类中设置真实内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM