Testing and validating REST services in Java is harder than in dynamic languages such as Ruby and Groovy. REST Assured brings the simplicity of using these languages into the Java domain. For example if your HTTP server returns the following JSON at “http://localhost:8080/lotto/{id}”:
Ruby 및 Groovy와 같은 동적 언어보다 Java에서 REST 서비스를 테스트하고 유효성을 검사하는 것은 더 어렵습니다. REST Assured는 이러한 언어를 Java 도메인에서
사용하는 간단한 방법을 제공합니다. 예를 들어 HTTP 서버가 "http : // localhost : 8080 / lotto / {id}"에 다음 JSON을 반환하는 경우 :
{
"lotto":{
"lottoId":5,
"winning-numbers":[2,45,34,23,7,5,3],
"winners":[
{
"winnerId":23,
"numbers":[2,45,34,23,3,5]
},
{
"winnerId":54,
"numbers":[52,3,12,11,18,22]
}
]
}
}
You can easily use REST Assured to validate interesting things from the response:
REST Assured를 사용하여 response에서 원하는 것을 검증 할 수 있습니다.
@Test public void
lotto_resource_returns_200_with_expected_id_and_winners() {
when().
get("/lotto/{id}", 5).
then().
statusCode(200).
body("lotto.lottoId", equalTo(5),
"lotto.winners.winnerId", containsOnly(23, 54));
}
Looks easy enough? Why not give it a spin? See getting starting and usage guide.
정말 쉽죠? 널리 알려 주세요? getting starting 및 usage guide를 참조하세요.
Getting Started
Maven / Gradle Users
Add the following dependency to your pom.xml:
아래 dependency를 pom.xml에 추가하세요.
REST Assured
Maven:
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.3</version>
<scope>test</scope>
</dependency>
Gradle:
testCompile 'io.rest-assured:rest-assured:3.0.3'
Notes
- You should place rest-assured before the JUnit dependency declaration in your pom.xml / build.gradle in order to make sure that the correct version of Hamcrest is used.
- REST Assured includes JsonPath and XmlPath as transitive dependencies
JsonPath
Standalone JsonPath (included if you depend on the rest-assured
artifact). Makes it easy to parse JSON documents. Note that this JsonPath implementation uses Groovy's GPath syntax and is not to be confused with Kalle Stenflo's JsonPath implementation.
Standalone JsonPath (rest-assured artifact에 의존하는 경우 포함됨). 이렇게 함으로서 JSON 문서를 쉽게 파싱 할 수 있습니다. 이 JsonPath 구현은 Groovy의 GPath 구문을 사용하므로 Jayway의 다른 JsonPath 구현과 혼동하지 않아야합니다.
Maven:
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>3.0.3</version>
</dependency>
Gradle:
compile 'io.rest-assured:json-path:3.0.3'
XmlPath
Stand-alone XmlPath (included if you depend on the rest-assured
artifact). Makes it easy to parse XML documents.
Stand-alone XmlPath (rest-assured artifact에 의존하는 경우 포함됨). 이렇게 함으로서 XML 문서를 쉽게 파싱 할 수 있습니다.
Maven:
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>xml-path</artifactId>
<version>3.0.3</version>
</dependency>
Gradle:
compile 'io.rest-assured:xml-path:3.0.3'
JSON Schema Validation
If you want to validate that a JSON response conforms to a Json Schema you can use the json-schema-validator
module:
JSON 응답이 Json Schema를 따르는 지 확인하려면 json-schema-validator 모듈을 사용할 수 있습니다.
Maven:
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>3.0.3</version>
<scope>test</scope>
</dependency>
Gradle:
testCompile 'io.rest-assured:json-schema-validator:3.0.3'
Refer to the documentation for more info.
자세한 내용은 설명서를 참조하십시오.
Spring Mock Mvc
If you're using Spring Mvc you can now unit test your controllers using the RestAssuredMockMvc API in the spring-mock-mvc module. For this to work you need to depend on the spring-mock-mvc
module:
Spring Mvc를 사용하고 있다면 Spring Mock-mvc 모듈의 RestAssuredMockMvc API를 사용하여 controllers를 유닛 테스트 할 수 있습니다. 이 작업을 하기 위해서는 spring-mock-mvc 모듈에 의존해야합니다.
Maven:
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>spring-mock-mvc</artifactId>
<version>3.0.3</version>
<scope>test</scope>
</dependency>
Gradle:
testCompile 'io.rest-assured:spring-mock-mvc:3.0.3'
Scala Support
If you're using Scala you may leverage the scala-support module. For this to work you need to depend on the scala-support
module:
Scala를 사용하는 경우 Scala 지원 모듈을 활용할 수 있습니다. 이 작업을 수행하려면 scala-support 모듈에 의존해야합니다.
SBT:
libraryDependencies += "io.rest-assured" % "scala-support" % "3.0.3"
Maven:
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>scala-support</artifactId>
<version>3.0.3</version>
<scope>test</scope>
</dependency>
Gradle:
testCompile 'io.rest-assured:scala-support:3.0.3'
Non-maven users
Download REST Assured and Json Schema Validator (optional). You can also download XmlPath and/or JsonPath separately if you don't need REST Assured. If you're using Spring Mvc then you can download the spring-mock-mvc module as well. If you're using Scala you may optionally download the scala-support module. Extract the distribution zip file and put the jar files in your class-path.
REST Assured 및 Json Schema Validator (선택 사항)를 다운로드하십시오. REST Assured가 필요하지 않은 경우 XmlPath 와 JsonPath를 별도로 다운로드 할 수도 있습니다. 당신이 Spring Mvc를 사용하고 있다면 당신은 spring-mock-mvc 모듈을 다운로드 할 수있습니다. Scala를 사용하는 경우 선택적으로 Scala 지원 모듈을 다운로드 할 수 있습니다. 배포 zip 파일의 압축을 풀고 jar 파일을 클래스 경로에 넣습니다.
Static imports
In order to use REST assured effectively it's recommended to statically import methods from the following classes:
REST assured를 효과적으로 사용하려면 다음 클래스에서 메소드를 정적으로 가져 오는 것이 좋습니다.
io.restassured.RestAssured.*
io.restassured.matcher.RestAssuredMatchers.*
org.hamcrest.Matchers.*
If you want to use Json Schema validation you should also statically import these methods:
Json Schema 유효성 검사를 사용하려면 다음 메소드를 정적으로 가져와야합니다.
io.restassured.module.jsv.JsonSchemaValidator.*
Refer to Json Schema Validation section for more info.
자세한 내용은 Json 스키마 유효성 검사 섹션을 참조하십시오.
If you're using Spring MVC you can use the spring-mock-mvc module to unit test your Spring Controllers using the Rest Assured DSL. To do this statically import the methods from RestAssuredMockMvc instead of importing the methods from io.rest-assured.RestAssured
and io.rest-assured.matcher.RestAssuredMatchers
:
Spring MVC를 사용한다면, Spring-mock-mvc 모듈을 사용하여 Rest Assured DSL을 사용하여 Spring Controllers를 테스트 할 수있다. 이렇게하려면 io.rest-assured.RestAssured 및 io.rest-assured.matcher.RestAssuredMatchers에서 메서드를 가져 오는 대신 RestAssuredMockMvc에서 메서드를 정적으로 가져옵니다.
io.restassured.module.mockmvc.RestAssuredMockMvc.*
io.restassured.matcher.RestAssuredMatchers.*
Version 2.x
If you need to depend on an older version replace groupId io.rest-assured
with com.jayway.restassured
.
이전 버전에 의존해야한다면 com.jayway.restassured로 groupId io.rest-assured를 교체하십시오.
Documentation
When you've successfully downloaded and configured REST Assured in your classpath please refer to the usage guide for examples.
클래스 패스에 REST Assured를 성공적으로 다운로드하고 구성한 경우 예제를 보려면 사용 설명서를 참조하십시오.
Usage Guide
Note that if you're using version 1.9.0 or earlier please refer to the legacy documentation.
버전 1.9.0 이전 버전을 사용하는 경우 기존 설명서를 참조하십시오.
REST Assured is a Java DSL for simplifying testing of REST based services built on top of HTTP Builder. It supports POST, GET, PUT, DELETE, OPTIONS, PATCH and HEAD requests and can be used to validate and verify the response of these requests.
REST Assured는 HTTP 빌더 위에 구축 된 REST 기반 서비스의 테스트를 단순화하기위한 Java DSL입니다. POST, GET, PUT, DELETE, OPTIONS, PATCH 및 HEAD 요청을 지원하며 이러한 요청의 응답을 확인하고 검증하는 데 사용할 수 있습니다.
Contents
- Static imports
- Examples
- JSON Example
- JSON Schema Validation
- XML Example
- Advanced
- XML
- JSON
- Additional Examples
- Note on floats and doubles
- Note on syntax (syntactic sugar)
- Getting Response Data
- Extracting values from the Response after validation
- JSON (using JsonPath)
- XML (using XmlPath)
- Single Path
- Headers, cookies, status etc
- Multi-value headers
- Multi-value cookies
- Detailed Cookies
- Specifying Request Data
- Invoking HTTP resources
- Parameters
- Multi-value Parameter
- No-value Parameter
- Path Parameters
- Cookies
- Headers
- Content-Type
- Request Body
- Verifying Response Data
- Response Body
- Cookies
- Status
- Headers
- Content-Type
- Full body/content matching
- Use the response to verify other parts of the response
- Measuring response time
- Authentication
- Basic
- Preemptive
- Challenged
- Digest
- Form
- CSRF
- OAuth
- OAuth1
- OAuth2
- Multi-part form data
- Object Mapping
- Serialization
- Content-Type based Serialization
- Create JSON from a HashMap
- Using an Explicit Serializer
- Deserialization
- Content-Type based Deserialization
- Custom Content-Type Deserialization
- Using an Explicit Deserializer
- Configuration
- Custom
- Parsers
- Custom
- Default
- Default Values
- Specification Re-use
- Filters
- Ordered Filters
- Response Builder
- Logging
- Request Logging
- Response Logging
- Log if validation fails
- Root Path
- Path Arguments
- Session Support
- Session Filter
- SSL
- SSL invalid hostname
- URL Encoding
- Proxy Configuration
- Static Proxy Configuration
- Request Specification Proxy Configuration
- Detailed configuration
- Encoder Config
- Decoder Config
- Session Config
- Redirect DSL
- Connection Config
- JSON Config
- HTTP Client Config
- SSL Config
- Param Config
- Spring Mock Mvc Module
- Bootstrapping RestAssuredMockMvc
- Asynchronous Requests
- Adding Request Post Processors
- Adding Result Handlers
- Using Result Matchers
- Interceptors
- Specifications
- Resetting RestAssuredMockMvc
- Spring MVC Authentication
- Note on parameters
- Scala Support Module
- Kotlin
- More Info
'TDD Project > Rest Assured' 카테고리의 다른 글
Specification Re-use in Rest-Assured with ResponseSpecBuilder and RequestSpecBuilder (0) | 2018.03.28 |
---|---|
[Hamcrest] The Hamcrest Tutorial (0) | 2017.05.19 |
[Hamcrest] Using Hamcrest for testing (0) | 2017.04.26 |