[英]How to access grailsApplication and ApplicationContext in Functional Tests
我正在構建一個Grails REST應用程序,並且我已經使用Spock編寫了我的功能測試。 請注意,這些功能測試僅適用於REST服務。
class LoginFunctionalSpec extends Specification {
def setUp() {
// here I want to access grailsApplication.config
}
}
我已經嘗試過以下方法:
但是他們都沒有在我的功能測試中提供grailsApplication.config。
有沒有辦法實現這個目標?
@dmahapatro
我沒有使用Geb,因為我正在測試REST服務,並且不需要瀏覽器自動化。
正如我在第1點中所說的那樣,grails應用程序沒有被自動注入。
class LoginFunctionalSpec extends Specification {
def grailsApplication
def setUp() {
def port = grailsApplication.config.mongo.port //throws NPE
}
}
它拋出一個NPE,說在null對象上調用了config屬性。
我認為你不需要明確注入任何東西。 默認情況下, grailsApplication
在測試中可用。
class LoginFunctionalSpec extends Specification {
def setUp() {
grailsApplication.config.blah //should work
}
}
注意-
Spock不提供功能測試的直接支持。 您需要使用Geb進行正確的功能測試。
您是在集成測試階段還是功能測試階段運行測試?
您需要從IntegrationSpec擴展到在集成測試階段進行自動裝配,但是您在這里談論功能測試...由於grails在與測試中的應用程序相同的JVM中運行測試,您實際上不需要使用RemoteControl但是我總是在我的功能測試中做,因為它對我來說似乎更干凈,並且如果測試中的測試和應用程序沒有在同一個JVM中運行,那么它是一個非常強大的技術。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.