简体   繁体   English

如何在功能测试中访问grailsApplication和ApplicationContext

[英]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. 我正在构建一个Grails REST应用程序,并且我已经使用Spock编写了我的功能测试。 Note that these functional tests are for REST services only. 请注意,这些功能测试仅适用于REST服务。

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. 注入grailsApplication(通过定义为类级别变量),但没有注入任何内容。
  2. Using ApplicationHolder.application.config but it has been deprecated and the suggested approach was the previous point. 使用ApplicationHolder.application.config但它已被弃用,建议的方法是前一点。
  3. Tried using plugin remote-control:1.4 but although grails console showed the plugin as installed I didn't find the class RemoteControl. 尝试使用插件远程控制:1.4虽然grails控制台显示插件已安装我没有找到类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. 我想读的grails-app / conf目录/ Config.groovy中为文件(如解释在这里 ),但有一个FileNotFoundException异常。

But none of them provide me grailsApplication.config inside my functional test. 但是他们都没有在我的功能测试中提供grailsApplication.config。

Is there any way to achieve this ? 有没有办法实现这个目标?

UPDATE UPDATE

@dmahapatro @dmahapatro

I am not using Geb because I am testing REST services and there is no browser automation required. 我没有使用Geb,因为我正在测试REST服务,并且不需要浏览器自动化。

As I said in point#1 grails Application is not being auto-injected. 正如我在第1点中所说的那样,grails应用程序没有被自动注入。

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. 它抛出一个NPE,说在null对象上调用了config属性。

I think you do not need to inject anything explicitly. 我认为你不需要明确注入任何东西。 grailsApplication is available in the tests by default. 默认情况下, grailsApplication在测试中可用。

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

Note- 注意-
Spock does not provide direct support for functional tests. Spock不提供功能测试的直接支持。 You need to use Geb for proper functional tests. 您需要使用Geb进行正确的功能测试。

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. 您需要从IntegrationSpec扩展到在集成测试阶段进行自动装配,但是您在这里谈论功能测试...由于grails在与测试中的应用程序相同的JVM中运行测试,您实际上不需要使用RemoteControl但是我总是在我的功能测试中做,因为它对我来说似乎更干净,并且如果测试中的测试和应用程序没有在同一个JVM中运行,那么它是一个非常强大的技术。

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

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