Access grailsApplication from spock unit test code in src / groovy

I am trying to test the code used in src / groovy with Spock. The code I'm testing refers to grailsApplication. Since it's in src / groovy, I used dependency injection to inject grailsApplication into a bean, for example:

ticketRequestEmailInfo(TicketRequestEmailInfo) {
    grailsApplication = ref('grailsApplication')
}

The problem I ran into is when the test code falls into a line of code that references grailsApplication, I get a NullPointerException:

java.lang.NullPointerException: Cannot get property 'config' on null object

My test class has the following:

@TestMixin(GrailsUnitTestMixin)
class TicketRequestEmailInfoSpec extends Specification {
    def setup() {
        grailsApplication.config.acme.purchase.trsUrlBase = "http://localhost:8082/purchase/order/"
}

Does anyone have any suggestions?

+4
source share
2 answers

Try Holders.grailsApplication.config:

@TestMixin(GrailsUnitTestMixin)
class TicketRequestEmailInfoSpec extends Specification {
    def setup() {
        Holders.grailsApplication.config.acme.purchase.trsUrlBase = "http://localhost:8082/purchase/order/"
}
+8
source

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/"
}
0

Source: https://habr.com/ru/post/1546608/


All Articles