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. |
io.rest-assured rest-assured 3.0.6 test |
import org.junit.Test; import static io.restassured.RestAssured.; import static io.restassured.matcher.ResponseAwareMatcher.; import static org.hamcrest.Matcher.*; public class Baidu { @Test public void testGetHtml(){ given() .log().all().get("https://www.baidu.com") .then() .log().all().statusCode(200); } } |
import static xx.xxx.*; |
import org.junit.Test; import static io.restassured.RestAssured.*; public class LocalInterface { @Test public void testlocal(){ given() .queryParam("username","Richered") .when() .get("http://localhost:8000/CGIProject/cgi-bin/") .then() .statusCode(200); } } |
given() |
given() |
io.rest-assured json-schema-validator 4.1.2 test |
@Test public void testPostJson(){ HashMap<String , Object> map = new HashMap<String ,Object>(); map.put("username","admin"); map.put("password","admin"); map.put("capcha","n6d58"); given().log().all() .contentType(ContentType.JSON) .body(map) .when() .post("http://localhost:8080/renren-fast/sys/login") .then() .statusCode(200); } |
{ "store":{ "book":[ { "author":"Nigel Rees", "category":"reference", "price":8.95, "title":"Sayings of the Century" }, { "author":"Evelyn Waugh", "category":"fiction", "price":12.99, "title":"Sword of Honour" }, { "author":"Herman Melville", "category":"fiction", "isbn":"0-553-21311-3", "price":8.99, "title":"Moby Dick" }, { "author":"J. R. R. Tolkien", "category":"fiction", "isbn":"0-395-19395-8", "price":22.99, "title":"The Lord of the Rings" } ] } } |
@Test public void TestAssertion(){ given() .contentType(ContentType.JSON) .when() .get("http://localhost:8000/CGIProject/cgi-bin/store.json") .then() .log().all() .body("store.book.findAll { it.price < 10 }.title",hasItems("Sayings of the Century", "Moby Dick")); } |
then(). body("store.book.author.collect { it.length() }.sum()", greaterThan(50)); |
def words = ['ant','buffalo','cat','dinosaur'] assert[3,6,3,8] ==words*.length() |
int sumOfAllAuthorLengths = from(response).getInt("store.book.author*.length().sum()"); assertThat(sumOfAllAuthorLengths, is(53)); |
given() .xxxxx .when() .xxxxx .then() .xxxxx |
InputStream stream = get("http://localhost:8000/CGIProject/cgi-bin/store.json").asInputStream(); byte[] byteArray = get("http://localhost:8000/CGIProject/cgi-bin/store.json").asByteArray(); String json = get("http://localhost:8000/CGIProject/cgi-bin/store.json").asString(); |
{ "title" : "My Title", "_links": { "self": { "href": "/title" }, "next": { "href": "/title?page=2" } } } |
@Test public void Relation(){ String nextTitleLink = given(). param("param_name","param_value"). when(). get("http://localhost:8000/CGIProject/cgi-bin/MyTitle.json"). then(). contentType(ContentType.JSON). body("title", equalTo("My Title")). extract(). path("_links.next.href"); System.out.println(nextTitleLink); given().log().all() .get("http://localhost:8000/CGIProject/cgi-bin/MyTitle.json" + nextTitleLink); } |
@Test public void Relation1(){ Response response = given(). param("param_name","param_value"). when(). get("http://localhost:8000/CGIProject/cgi-bin/MyTitle.json"). then(). contentType(ContentType.JSON). body("title", equalTo("My Title")). extract(). response(); String nextTitleLink = response.path("_links.next.href"); String headerValue = response.header("headerName"); } |
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |