我试图使用Spock测试src / groovy中的一些代码。我测试的代码引用了grailsApplication。因为它在src / groovy中,我使用依赖注入将grailsApplication注入bean中,如下所示:
ticketRequestEmailInfo(TicketRequestEmailInfo) {
grailsApplication = ref('grailsApplication')
}
我遇到的问题是当测试代码遇到引用grailsApplication的代码行时,我得到了NullPointerException:
java.lang.NullPointerException: Cannot get property 'config' on null object
我的测试类有以下内容:
@TestMixin(GrailsUnitTestMixin)
class TicketRequestEmailInfoSpec extends Specification {
def setup() {
grailsApplication.config.acme.purchase.trsUrlBase = "http://localhost:8082/purchase/order/"
}
有人有任何建议吗?
答案 0 :(得分:8)
尝试Holders.grailsApplication.config
:
@TestMixin(GrailsUnitTestMixin)
class TicketRequestEmailInfoSpec extends Specification {
def setup() {
Holders.grailsApplication.config.acme.purchase.trsUrlBase = "http://localhost:8082/purchase/order/"
}
答案 1 :(得分:0)
没有注入grailsApplication
作为def grailsApplication
进行测试?
您也可以将其导入为import static grails.util.Holders.config as grailsConfig
。
然后将其用作
@TestMixin(GrailsUnitTestMixin)
class TicketRequestEmailInfoSpec extends Specification {
def setup() {
grailsConfig.acme.purchase.trsUrlBase = "http://localhost:8082/purchase/order/"
}