在 Grails 应用程序中使用 UnitSpec 为服务类运行 spock 测试用例时,将 grailsApplication 设为 null。
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")
}
在 AccountService 类 getAccountByStatus() 方法中,使用 url = grailsApplication.config.ACCOUNTWEBSERVICEURL 调用 webservice,其中有 default.properties 文件,但是当我运行 spock 测试用例时,它会抛出类似的错误
无法在空对象上获取属性“配置”