Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing grailsApplication from spock unit test testing code in src/groovy

I'm trying to test some code that's in src/groovy using Spock. The code I'm testing references grailsApplication. Since it's in src/groovy, I used dependency injection to inject grailsApplication into the bean, like so:

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

The problem I'm having is when the test code hits the line of code that references grailsApplication, I'm getting 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?

like image 991
user1866924 Avatar asked Feb 12 '23 15:02

user1866924


1 Answers

Try Holders.grailsApplication.config:

@TestMixin(GrailsUnitTestMixin)
class TicketRequestEmailInfoSpec extends Specification {
    def setup() {
        Holders.grailsApplication.config.acme.purchase.trsUrlBase = "http://localhost:8082/purchase/order/"
}
like image 102
nickdos Avatar answered Feb 15 '23 07:02

nickdos