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

2

5

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?

Start answered 30/6, 2014 at 19:0 Comment(0)
S
8

Try Holders.grailsApplication.config:

@TestMixin(GrailsUnitTestMixin)
class TicketRequestEmailInfoSpec extends Specification {
    def setup() {
        Holders.grailsApplication.config.acme.purchase.trsUrlBase = "http://localhost:8082/purchase/order/"
}
Sopping answered 1/7, 2014 at 1:39 Comment(0)
P
0

Didn't works injecting grailsApplication to test as def grailsApplication ?

Also you can import it as import static grails.util.Holders.config as grailsConfig.

And then use it as

@TestMixin(GrailsUnitTestMixin)
class TicketRequestEmailInfoSpec extends Specification {
    def setup() {
        grailsConfig.acme.purchase.trsUrlBase = "http://localhost:8082/purchase/order/"
}
Polymer answered 1/7, 2014 at 18:27 Comment(1)
Using def grailsApplication in the test gives the NullPointerException. Must be because the code I'm testing is in src/groovy. Using Holders.grailsApplication.config as suggested above works though.Start

© 2022 - 2024 — McMap. All rights reserved.