rest-assured : Restful API 测试利器

{
  "Author":"tomcat and jerry",
  "URL" :"http://www.cnblogs.com/tomcatandjerry/p/5885028.html"
}

这里先贴一下IBM上的介绍

http://www.ibm.com/developerworks/cn/java/j-lo-rest-assured/index.html

Java 程序员常常借助于 JUnit 来测试自己的 REST API,不,应该这样说,Java 程序员常常借助于 JUnit 来测试 REST API 的实现!从某种角度来说,这是一种“白盒测试”,Java 程序员清楚地知道正在测试的是哪个类、哪个方法,而不是从用户的角度出发,测试的是哪个 REST API。

不废话,直接给Spring-Boot项目配置:

import static com.jayway.restassured.RestAssured.given;

import org.apache.logging.log4j.LogManager;
import org.hamcrest.Matcher;
import org.junit.After;
import org.mockito.internal.matchers.Equals;
import org.mockito.internal.matchers.GreaterThan;
import org.mockito.internal.matchers.NotNull;
import org.mockito.internal.matchers.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jayway.restassured.matcher.ResponseAwareMatcher;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.response.ValidatableResponse;

@RunWith(SpringJUnit4ClassRunner.class)   //1.
@SpringApplicationConfiguration(classes = YourApp.class)   // 2.SpringBoot入口类
@WebIntegrationTest("server.port:0")   // 3: server.port:0 > 指定服务器端口,为0表示随机端口
public class YourControllerTest{

    @Value("${local.server.port}")   //4
    int port;

    @Before
    public void doBefore(){
        RestAssured.port = port;
    }
    
    @Test
    public void firstTest(){
        JSONObject parm = new JSONObject();
        parm.put("userId", "your id");
        parm.put("name", "your name");
        ValidatableResponse response = given().contentType("application/json")
        .request().body(parm.toJSONString())
        .when().post("/your/restful/api")
        .then()
        .body("return_code", new ResponseAwareMatcher<Response>() {
            @Override
            public Matcher<?> matcher(Response response) throws Exception {
                System.out.println(response.prettyPrint());//获取返回的json数据(1),举例:{"return_code":0,"return_msg":"success"}
                return new Equals(0);
            }
        })
        .body("return_code",new Equals(0));
        ;
        JSONObject json = JSONObject.parseObject(response.extract().asString());//获取返回的json数据(2)
        //自己写一些代码
    
    }

}

pom.xml引入jar

<dependency>
            <groupId>com.jayway.restassured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>2.3.3</version>
            <scope>test</scope>
        </dependency>

该测试工具跟Spring-Boot简直绝配,跑测试用例时,会启动SpringBoot(你配置的YourApp.java),等启动完成,会跑testcase,最后会关闭服务器。

真正的黑盒+单元测试,让restful API更高质量。

Spring-boot 更新到1.4.1
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
</parent>
@RunWith(SpringJUnit4ClassRunner.class)   //1.
@SpringBootTest(classes = Application.class, webEnvironment=WebEnvironment.RANDOM_PORT )   // 2.SpringBoot入口类,配置起server随机端口
public class RestTest {

    @Value("${local.server.port}")   //3
    int port;

    @Before
    public void doBefore(){
        RestAssured.port = port;//4: 告诉restAssured使用哪个端口来访问
    }
}
原文地址:https://www.cnblogs.com/tomcatandjerry/p/5885028.html