简体   繁体   中英

How to access grailsApplication and ApplicationContext in Functional Tests

I am building a Grails REST application and I have written my Functional Tests using Spock. Note that these functional tests are for REST services only.

class LoginFunctionalSpec extends Specification {
  def setUp() {
    // here I want to access grailsApplication.config
  }
}

I have already tried the following approaches:

  1. Injecting grailsApplication (by defining as class level variable), but nothing get's injected.
  2. Using ApplicationHolder.application.config but it has been deprecated and the suggested approach was the previous point.
  3. Tried using plugin remote-control:1.4 but although grails console showed the plugin as installed I didn't find the class RemoteControl. Also I was never sure of this approach
  4. I tried reading grails-app/conf/Config.groovy as a file (as explained here ) but got a FileNotFoundException.

But none of them provide me grailsApplication.config inside my functional test.

Is there any way to achieve this ?

UPDATE

@dmahapatro

I am not using Geb because I am testing REST services and there is no browser automation required.

As I said in point#1 grails Application is not being auto-injected.

class LoginFunctionalSpec extends Specification {
  def grailsApplication

  def setUp() {
     def port = grailsApplication.config.mongo.port //throws NPE
  }
}

It throws a NPE saying that config property is being invoked on null object.

I think you do not need to inject anything explicitly. grailsApplication is available in the tests by default.

class LoginFunctionalSpec extends Specification {
  def setUp() {
     grailsApplication.config.blah //should work
  }
}

Note-
Spock does not provide direct support for functional tests. You need to use Geb for proper functional tests.

Are you running your test in integration test phase or functional test phase?

You need to extend from IntegrationSpec to get autowiring in integraton test phase but you are talking about functional tests here... Because of grails running your tests in the same JVM as the application under tests you don't really need to use RemoteControl but I always do anyway in my functional tests as it seems much cleaner to me and is a really powerful technique if your tests and application under tests are not running in the same JVM.

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