Chetan
Chetan

Reputation: 1707

How to write a Integration test in Grails for Domain Class as a REST resource?

I am using a Domain Class as a rest resource as mentioned in Grails documentation

So while using this approach there are no controllers or service classes created. And I tried to find a way to write Integration tests for the Domain as a REST resource but could not find it. So kindly tell me how to do it or post a link to somewhere which tells the same.

Regarding this approach, well it is the exact approach asked to make so I cannot change to other way of using REST services.

Thanks in advance.

Upvotes: 0

Views: 285

Answers (1)

roeygol
roeygol

Reputation: 5048

You should consider using Spock as your test framework:
https://spock-framework.readthedocs.org/en/latest/

Example how to use it for REST:

class ExampleWebAppSpecification extends Specification {
    def "Should return 200 & a message with the input appended"() {

        setup:
             def primerEndpoint = new RESTClient( 'http://localhost:8080/' )
        when:
             def resp = primerEndpoint.get([ path: 'exampleendpoint', query : [ input : 'Get a hair cut' ]])
        then:
             with(resp) {
                 status == 200
                 contentType == "application/json"
             }
             with(resp.data) {
                 payload == "Something really important: Get a hair cut"
             }
    }
}

EDIT - In the buildConfig.groovy:

compile("org.codehaus.groovy:groovy-all:2.2.0")
compile("com.fasterxml.jackson.core:jackson-databind")

testCompile("org.spockframework:spock-core:0.7-groovy-2.0")
testCompile("org.codehaus.groovy.modules.http-builder:http-builder:0.7+")
testCompile("net.sf.json-lib:json-lib:2.4+")

Upvotes: 1

Related Questions