{"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 - Java\u63a5\u53e3\u81ea\u52a8\u5316\u6d4b\u8bd5\u4ece0\u52301<\/h1>\n\n&gt; \u4f5c\u8005\uff1aJava\u6d4b\u5f00\n---\n\n<h2>\u524d\u8a00<\/h2>\n\n\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\n\n---\n\n<h2>\u4e00\u3001RestAssured\u7b80\u4ecb<\/h2>\n\n<h3>\u7279\u70b9<\/h3>\n\n<ul><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\n<code>`<\/code>xml\n\n    io.rest-assured\n    rest-assured\n    5.3.0\n\n<code>`<\/code>\n\n---\n\n<h2>\u4e8c\u3001\u57fa\u7840\u7528\u6cd5<\/h2>\n\n<h3>GET\u8bf7\u6c42<\/h3>\n\n<code>`<\/code>java\nimport static io.restassured.RestAssured.*;\n\n@Test\npublic void testGetUser() {\n    given()\n        .baseUri(\"https:\/\/api.example.com\")\n        .header(\"Authorization\", \"Bearer token\")\n    .when()\n        .get(\"\/users\/1\")\n    .then()\n        .statusCode(200)\n        .body(\"name\", equalTo(\"\u5f20\u4e09\"));\n}\n<code>`<\/code>\n\n<h3>POST\u8bf7\u6c42<\/h3>\n\n<code>`<\/code>java\n@Test\npublic void testCreateUser() {\n    String body = \"{\"name\":\"\u6d4b\u8bd5\u7528\u6237\",\"email\":\"test@example.com\"}\";\n\n    given()\n        .contentType(\"application\/json\")\n        .body(body)\n    .when()\n        .post(\"\/users\")\n    .then()\n        .statusCode(201)\n        .body(\"id\", notNullValue());\n}\n<code>`<\/code>\n\n---\n\n<h2>\u4e09\u3001\u65ad\u8a00\u8be6\u89e3<\/h2>\n\n<code>`<\/code>java\n.then()\n    \/\/ \u72b6\u6001\u7801\u65ad\u8a00\n    .statusCode(200)\n    \/\/ \u54cd\u5e94\u65f6\u95f4\u65ad\u8a00\n    .time(lessThan(1000L))\n    \/\/ JSON\u8def\u5f84\u65ad\u8a00\n    .body(\"code\", equalTo(200))\n    .body(\"data.size()\", greaterThan(0))\n    .body(\"data[0].name\", containsString(\"\u5f20\"))\n    \/\/ Header\u65ad\u8a00\n    .header(\"Content-Type\", containsString(\"application\/json\"));\n<code>`<\/code>\n\n---\n\n<h2>\u56db\u3001\u6570\u636e\u63d0\u53d6<\/h2>\n\n<code>`<\/code>java\n\/\/ \u63d0\u53d6\u5355\u4e2a\u503c\nString token = given()\n    .body(\"{\"username\":\"admin\",\"password\":\"123456\"}\")\n.when()\n    .post(\"\/login\")\n.then()\n    .extract().path(\"data.token\");\n\n\/\/ \u63d0\u53d6\u5b8c\u6574\u54cd\u5e94\nResponse response = given()\n    .get(\"\/users\")\n.then()\n    .extract().response();\n\nList names = response.jsonPath().getList(\"data.name\");\n<code>`<\/code>\n\n---\n\n<h2>\u603b\u7ed3<\/h2>\n\nRestAssured\u6838\u5fc3\u8981\u70b9\uff1a\n\n| \u529f\u80fd | \u7528\u6cd5 |\n|------|------|\n| GET\u8bf7\u6c42 | given().when().get() |\n| POST\u8bf7\u6c42 | given().body().post() |\n| \u65ad\u8a00 | then().body().statusCode() |\n| \u63d0\u53d6 | extract().path() |\n\n---\n\n<strong>\u4e0b\u671f\u9884\u544a\uff1aCucumber\u884c\u4e3a\u9a71\u52a8\u6d4b\u8bd5\u5165\u95e8 - \u8ba9\u6d4b\u8bd5\u7528\u4f8b\u4eba\u4eba\u90fd\u80fd\u8bfb\u61c2<\/strong>\n\n&gt; \u4f5c\u8005\uff1aJava\u6d4b\u5f00\uff0c5\u5e74Java\u63a5\u53e3\u6d4b\u8bd5\u7ecf\u9a8c","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\u3002RestAssured\u662fJava\u751f\u6001&#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}]}}