REST-assured API中then()方法的作用及正式定义是什么?
Hey! I get why you might have trouble finding the then() method in that specific RestAssured class Javadoc—it’s not actually defined there. Let me walk you through what it does and where it lives in the REST-assured ecosystem.
then() Method Does in REST-assured First, quick context: You won’t spot then() in the RestAssured class docs because it’s part of the response handling API, specifically defined in the Response interface (and its concrete implementations like ValidatableResponseImpl). Here’s its core purpose and functionality:
- Kicks off response validation: After you send a request using
given()/when(),then()transitions your test into the verification phase. It’s the point where you start checking that the API returned the expected results. - Enables fluent, chainable assertions: You can string together multiple checks in a single readable chain. For example:
given() .baseUrl("https://api.example.com") .when() .get("/products/123") .then() .statusCode(200) // Verify HTTP status code .body("price", lessThan(50.0)) // Check a JSON body field .header("Cache-Control", equalTo("max-age=3600")); // Validate a response header - Grants access to full response details: Under the hood,
then()exposes the entire response object, so you can validate status codes, headers, cookies, response body content (JSON/XML), and even response time metrics. - Works with Hamcrest matchers: It integrates seamlessly with Hamcrest matchers, which lets you write expressive, human-readable assertions instead of clunky conditional checks.
In short, then() is your go-to method for turning a raw API response into verified test results—it’s where you confirm that the API behaves as intended after you’ve made the call.
内容的提问来源于stack exchange,提问作者Skywarp

