{"id":36,"date":"2026-05-07T10:00:00","date_gmt":"2026-05-07T02:00:00","guid":{"rendered":"http:\/\/localhost:8888\/?p=36"},"modified":"2026-07-09T16:17:45","modified_gmt":"2026-07-09T08:17:45","slug":"restassured%e6%a1%86%e6%9e%b6%e7%b2%be%e8%ae%b2","status":"publish","type":"post","link":"https:\/\/blog.runnergo.cn\/?p=36","title":{"rendered":"RestAssured\u6846\u67b6\u7cbe\u8bb2 &#8211; Java\u63a5\u53e3\u81ea\u52a8\u5316\u6d4b\u8bd5\u4ece0\u52301"},"content":{"rendered":"<h1>RestAssured\u6846\u67b6\u7cbe\u8bb2 &#8211; Java\u63a5\u53e3\u81ea\u52a8\u5316\u6d4b\u8bd5\u4ece0\u52301<\/h1>\n<p>&gt; \u4f5c\u8005\uff1aJava\u6d4b\u5f00<br \/>\n&#8212;<\/p>\n<h2>\u524d\u8a00<\/h2>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u662fJava\u6d4b\u5f00\uff0c\u505aJava\u63a5\u53e3\u81ea\u52a8\u53165\u5e74\u3002RestAssured\u662fJava\u751f\u6001\u4e2d\u6700\u6d41\u884c\u7684\u63a5\u53e3\u6d4b\u8bd5\u6846\u67b6\uff0c\u4eca\u5929\u5206\u4eabRestAssured\u7684\u5b9e\u6218\u7ecf\u9a8c\u3002<\/p>\n<p>&#8212;<\/p>\n<h2>\u4e00\u3001RestAssured\u7b80\u4ecb<\/h2>\n<h3>\u7279\u70b9<\/h3>\n<ul>\n<li>DSL\u98ce\u683cAPI\uff0c\u8bed\u6cd5\u4f18\u96c5<\/li>\n<li>\u652f\u6301Given-When-Then\u7ed3\u6784<\/li>\n<li>\u5185\u7f6eJSON\/XML\u89e3\u6790<\/li>\n<li>\u4e0eJUnit\u3001TestNG\u65e0\u7f1d\u96c6\u6210<\/li>\n<\/ul>\n<h3>\u4f9d\u8d56\u914d\u7f6e<\/h3>\n<p><code>`<\/code>xml<\/p>\n<p>    io.rest-assured<br \/>\n    rest-assured<br \/>\n    5.3.0<\/p>\n<p><code>`<\/code><\/p>\n<p>&#8212;<\/p>\n<h2>\u4e8c\u3001\u57fa\u7840\u7528\u6cd5<\/h2>\n<h3>GET\u8bf7\u6c42<\/h3>\n<p><code>`<\/code>java<br \/>\nimport static io.restassured.RestAssured.*;<\/p>\n<p>@Test<br \/>\npublic void testGetUser() {<br \/>\n    given()<br \/>\n        .baseUri(&#8220;https:\/\/api.example.com&#8221;)<br \/>\n        .header(&#8220;Authorization&#8221;, &#8220;Bearer token&#8221;)<br \/>\n    .when()<br \/>\n        .get(&#8220;\/users\/1&#8221;)<br \/>\n    .then()<br \/>\n        .statusCode(200)<br \/>\n        .body(&#8220;name&#8221;, equalTo(&#8220;\u5f20\u4e09&#8221;));<br \/>\n}<br \/>\n<code>`<\/code><\/p>\n<h3>POST\u8bf7\u6c42<\/h3>\n<p><code>`<\/code>java<br \/>\n@Test<br \/>\npublic void testCreateUser() {<br \/>\n    String body = &#8220;{&#8220;name&#8221;:&#8221;\u6d4b\u8bd5\u7528\u6237&#8221;,&#8221;email&#8221;:&#8221;test@example.com&#8221;}&#8221;;<\/p>\n<p>    given()<br \/>\n        .contentType(&#8220;application\/json&#8221;)<br \/>\n        .body(body)<br \/>\n    .when()<br \/>\n        .post(&#8220;\/users&#8221;)<br \/>\n    .then()<br \/>\n        .statusCode(201)<br \/>\n        .body(&#8220;id&#8221;, notNullValue());<br \/>\n}<br \/>\n<code>`<\/code><\/p>\n<p>&#8212;<\/p>\n<h2>\u4e09\u3001\u65ad\u8a00\u8be6\u89e3<\/h2>\n<p><code>`<\/code>java<br \/>\n.then()<br \/>\n    \/\/ \u72b6\u6001\u7801\u65ad\u8a00<br \/>\n    .statusCode(200)<br \/>\n    \/\/ \u54cd\u5e94\u65f6\u95f4\u65ad\u8a00<br \/>\n    .time(lessThan(1000L))<br \/>\n    \/\/ JSON\u8def\u5f84\u65ad\u8a00<br \/>\n    .body(&#8220;code&#8221;, equalTo(200))<br \/>\n    .body(&#8220;data.size()&#8221;, greaterThan(0))<br \/>\n    .body(&#8220;data[0].name&#8221;, containsString(&#8220;\u5f20&#8221;))<br \/>\n    \/\/ Header\u65ad\u8a00<br \/>\n    .header(&#8220;Content-Type&#8221;, containsString(&#8220;application\/json&#8221;));<br \/>\n<code>`<\/code><\/p>\n<p>&#8212;<\/p>\n<h2>\u56db\u3001\u6570\u636e\u63d0\u53d6<\/h2>\n<p><code>`<\/code>java<br \/>\n\/\/ \u63d0\u53d6\u5355\u4e2a\u503c<br \/>\nString token = given()<br \/>\n    .body(&#8220;{&#8220;username&#8221;:&#8221;admin&#8221;,&#8221;password&#8221;:&#8221;123456&#8243;}&#8221;)<br \/>\n.when()<br \/>\n    .post(&#8220;\/login&#8221;)<br \/>\n.then()<br \/>\n    .extract().path(&#8220;data.token&#8221;);<\/p>\n<p>\/\/ \u63d0\u53d6\u5b8c\u6574\u54cd\u5e94<br \/>\nResponse response = given()<br \/>\n    .get(&#8220;\/users&#8221;)<br \/>\n.then()<br \/>\n    .extract().response();<\/p>\n<p>List names = response.jsonPath().getList(&#8220;data.name&#8221;);<br \/>\n<code>`<\/code><\/p>\n<p>&#8212;<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>RestAssured\u6838\u5fc3\u8981\u70b9\uff1a<\/p>\n<p>| \u529f\u80fd | \u7528\u6cd5 |<br \/>\n|&#8212;&#8212;|&#8212;&#8212;|<br \/>\n| GET\u8bf7\u6c42 | given().when().get() |<br \/>\n| POST\u8bf7\u6c42 | given().body().post() |<br \/>\n| \u65ad\u8a00 | then().body().statusCode() |<br \/>\n| \u63d0\u53d6 | extract().path() |<\/p>\n<p>&#8212;<\/p>\n<p><strong>\u4e0b\u671f\u9884\u544a\uff1aCucumber\u884c\u4e3a\u9a71\u52a8\u6d4b\u8bd5\u5165\u95e8 &#8211; \u8ba9\u6d4b\u8bd5\u7528\u4f8b\u4eba\u4eba\u90fd\u80fd\u8bfb\u61c2<\/strong><\/p>\n<p>&gt; \u4f5c\u8005\uff1aJava\u6d4b\u5f00\uff0c5\u5e74Java\u63a5\u53e3\u6d4b\u8bd5\u7ecf\u9a8c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>RestAssured\u6846\u67b6\u7cbe\u8bb2 &#8211; Java\u63a5\u53e3\u81ea\u52a8\u5316\u6d4b\u8bd5\u4ece0\u52301 &gt; \u4f5c\u8005\uff1aJava\u6d4b\u5f00 &#8212; \u524d\u8a00 \u5927\u5bb6\u597d\uff0c\u6211\u662fJava\u6d4b\u5f00\uff0c\u505aJava\u63a5\u53e3\u81ea\u52a8\u53165\u5e74\u3002RestAssu&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-36","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=\/wp\/v2\/posts\/36","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=36"}],"version-history":[{"count":0,"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=\/wp\/v2\/posts\/36\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}