[英]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: 我已经尝试过以下方法:
But none of them provide me grailsApplication.config inside my functional test. 但是他们都没有在我的功能测试中提供grailsApplication.config。
Is there any way to achieve this ? 有没有办法实现这个目标?
@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.