在Grails应用程序中使用UnitSpec for service class运行spock测试用例时,将grailsApplication设置为null。无法在空对象上获取属性“配置” - Grails服务Spock测试
Error - Cannot get property 'config' on null object
有人可以告诉我如何配置grailsApplication
,而spock测试服务类。
我google了很多,但没有解决我的问题。
这是代码。
def accountServiceMock = Mock(AccountService)
def accountClientService = new AccountClientService()
def setup(){
accountClientService.accountWS = accountServiceMock
accountClientService.basicAuthInterceptor = authenticatorServiceMock
}
def "test account by status() "(){
setup:
def mockAccountStatus = "ACTIVE"
mockDomain(Account, [accountInstance])
accountClientService.grailsApplication = grailsApplication
when:
accountClientService.getAccountByStatus(mockAccountStatus) //calling web service by fetching url from default.properties file which is context
then:
Account.count() != 0
where:
accountInstance = new Account(10L, "ACTIVE","1234", "firstName", "LastName")
}
在帐户服务类getAccountByStatus()方法调用与web服务的url = grailsApplication.config.ACCOUNTWEBSERVICEURL这是有default.properties中的文件 但是当我运行斯波克测试情况下,抛出错误,如
无法获得财产 '配置' 空对象
您可以发布您的测试代码,也许有助于看看发生了什么事情。 – 2013-03-14 13:52:42
哪个Grails版本?哪个Spock版本? – 2013-03-15 06:32:54
你在做单元测试或集成测试吗?通过它的外观,你想做一个集成测试。如果这是真的看这里http://grails.org/doc/2.2.0/guide/single.html#integrationTesting正确实施测试 – Bart 2013-03-15 14:16:36