REST确保Spock和Groovy作为集成测试

时间:2016-04-19 13:09:25

标签: groovy integration-testing spock rest-assured

在spock框架中使用REST保证的框架。 这是关闭this closed topic

的答案

1 个答案:

答案 0 :(得分:8)

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))
    }
}