4

Using REST-assured framework in spock framework. This is answer to closed this closed topic

Community
  • 1
  • 1
librucha
  • 635
  • 8
  • 13
  • Possible duplicate of [REST-assured with Spock and Groovy](http://stackoverflow.com/questions/25848889/rest-assured-with-spock-and-groovy) – jstrater Apr 19 '16 at 14:46

1 Answers1

10
package cz.audatex.audanext.auth.endpoint.v1.member.settings

import com.jayway.restassured.RestAssured
import org.librucha.ServerApp
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.SpringApplicationConfiguration
import org.springframework.boot.test.WebIntegrationTest
import spock.lang.Specification
import spock.lang.Unroll

@SpringApplicationConfiguration(classes = ServerApp.class)
@WebIntegrationTest(randomPort = true)
class Test extends Specification {

    @Value('${local.server.port}')
    private int port

    def setup() throws Exception {
        RestAssured.port = port
    }

    @Unroll
    def "Get services as '#accessor.role' [#iterationCount]"() {
        given:
            def request = given()
                 .accept(JSON)
                 .auth().oauth2(getToken(accessor.name))
                 .log().all()
        when:
            def response = request.with().get("/api/v1/services")
        then:
            response.then().log().all()
                 .statusCode(status)
                 .spec(specification)
        where:
            accessor                                    || status       || specification
            [name: 'user-login', role: 'ordinary user'] || SC_FORBIDDEN || expect().body('code', equalTo('access_denied'), 'description', equalTo('Access is denied'))
            [name: 'admin-login', role: 'super admin']  || SC_OK        || expect().body('id', contains(1, 2, 3, 4))
    }
}
librucha
  • 635
  • 8
  • 13
  • Looks pretty nice. Have you examples of more complicated tests? Does it become less useful with big test or large amount of input data? – RocketRaccoon Jun 21 '16 at 13:25
  • @RocketRaccoon Sorry this project was commercial and my contract don't allow me publish code. But I used it for whole REST integration test including authorization with OAuth2. – librucha Sep 14 '16 at 07:43