If you’ve made the proper pom.xml entries we went over at the top of this post, you should be able to begin using REST-Assured.
- In your IDE, create a new java class named WordPress_API
- Import the following:
import org.junit.Test; import org.junit.Before; import static com.jayway.restassured.RestAssured.expect; import static org.hamcrest.Matchers.equalTo;
- Create a public method named simpleExample
- Add the junit @Test annotation above it.
- Inside simpleExample, add the following code:
expect().body(“title”, equalTo(“Hello world!”)).when().get(“http://localhost:64217/wp-json.php/posts/1”);
- Your code should look like the following:
Now — just to prove that this is really working, let’s force it to fail so that we can be confident that everything’s kosher.
- Change the Hello world! To Hello JOE!
- Rerun the test.
- The test should now fail.
The REST-assured syntax is pretty flexible. For example, we could have also written our test this way:
RestAssured.baseURI = “http://localhost:64217/”;
RestAssured.get(“wp-json.php/posts/1”).then().assertThat().body(“title”, equalTo(“Hello world!”));
Also, if you’re familiar with Selenium, REST-Assured can also use hamcrest for testing using hamcrest’s library of matchers for building test expressions.
Comments
Post a Comment