简体   繁体   中英

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)

code(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. it seems like I have to inflate the grails application somewhere?

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?. 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:

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.

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. Subclassing IntegrationSpec from Spock will inject the grailsApplication into your test, so you can set the real thing in your Attack class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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