Spock is a testing and specification framework for Java and Groovy applications. Here we will use groovy RestClient along with Spock to test Restful Services.
import groovyx.net.http.RESTClient
import spock.lang.Specification
import spock.lang.Shared
class MyApiSpec extends Specification {
@Shared RESTClient restClient
def setupSpec() {
restClient = new RESTClient('hostnamewithport')
restClient.handler.failure = restClient.handler.success
}
def 'test get method'(){
given:
restClient.headers.Accept = 'application/json'
when:
def resp = restClient .get(path: 'path(ex:/api/list/'),
query:[param1:'param1value',param2:'param2value']
)
then:
resp.status == 200
resp.data.somejsonobject == 'expectedjson'
}
def 'test post method'(){
given:
restClient .headers.Accept = 'application/json'
when:
def resp = restClient .post(path: 'path(ex:/api/list/',
query:[param1:'param1value',param2:'param2value'],
body: 'your json',
contentType:'application/json'
)
then:
resp.status == 200
}
}
Comments
Post a Comment