04. Junit, Rest Assure - YAPP-16th/Team_Web_3_Backend GitHub Wiki

04. Junit, Rest Assured ํ…Œ์ŠคํŠธ

REST Assured๋Š” REST ์›น ์„œ๋น„์Šค๋ฅผ ๊ฒ€์ฆํ•˜๊ธฐ ์œ„ํ•œ Java ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค.

HTTP endpoint์— ๋Œ€ํ•œ ์—ฐ๊ฒฐ ๋ฐ ์˜ˆ์ƒ๋˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์„ค๋ช…ํ•˜๋Š” ์นœ์ˆ™ํ•œ DSL (Domain Specific Languages)์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

import org.junit.Test;
import io.restassured.RestAssured;
public class SampleTest {
    @Test
    public void test() {
        RestAssured.given()
                    .when().get("http://www.google.com")
                    .then().statusCode(200);
    }
}

์ด JUnit ํ…Œ์ŠคํŠธ๋Š” Google์— ์—ฐ๊ฒฐํ•˜๊ณ , GET ํ˜ธ์ถœ์„ ์ˆ˜ํ–‰ํ•˜๋ฉฐ, HTTP ์ฝ”๋“œ 200/success๊ฐ€ ๋ฐ˜ํ™˜๋˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

์ผ๋ฐ˜์ ์ธ JUnit assert ๋ฌธ์ด ์—†์Šต๋‹ˆ๋‹ค.

REST Assured๋Š” ๋ฐ˜ํ™˜๋œ status code์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ pass ๋˜๋Š” fail์„ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค.

REST Assured ์„ค์ • pom.xml ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ Dependency๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    <dependencies>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>3.0.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

GSON์€ API๋ฅผ Request Body์— Jsonํ˜•์‹์œผ๋กœ ํ˜ธ์ถœํ•  ๋•Œ, ์ž๋™์œผ๋กœ Json ํ˜•์‹์œผ๋กœ ํ˜ธ์ถœํ•˜๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค.

Reference

โš ๏ธ **GitHub.com Fallback** โš ๏ธ