Rest Assured
Rest Assured
Prerequisites Verify JSON via GET JsonPath Groovy Closures The JSON
File Uploads
Registering Custom Parsers
Specification Reuse
Were going to need a JDK and REST-Assured jars.. Java Development Kit >= 6 The full tutorial and the RESTful Webservice to test can be found at my blog www.hascode.com
{ "email":"[email protected]", "firstName":"Tim", "id":"1", "lastName":"Testerman" } @Test public void testGetSingleUser() { expect(). statusCode(200). body( "email", equalTo("[email protected]"), "firstName", equalTo("Tim"), "lastName", equalTo("Testerman"), "id", equalTo("1")). when(). get("/service/single-user"); }
{ "email":"[email protected]", "firstName":"Tim", "id":"1", "lastName":"Testerman" } @Test public void testGetSingleUserProgrammatic() { Response res = get("/service/single-user"); assertEquals(200, res.getStatusCode()); String json = res.asString(); JsonPath jp = new JsonPath(json); assertEquals("[email protected]", jp.get("email")); assertEquals("Tim", jp.get("firstName")); assertEquals("Testerman", jp.get("lastName")); assertEquals("1", jp.get("id")); }
{ "person":[ { "@id":"1", "email":"[email protected]", "firstName":"Tim", "lastName":"Testerman" },{ "@id":"20", "email":"[email protected]", "firstName":"Sara", "lastName":"Stevens" },{ "@id":"11", "email":"[email protected]", "firstName":"Mark", "lastName":"Mustache" } ] }
@Test public void testFindUsingGroovyClosure() { String json = get("/service/persons/json").asString(); JsonPath jp = new JsonPath(json); jp.setRoot("person"); Map person = jp.get("find {e -> e.email =~ /test@/}"); assertEquals("[email protected]", person.get("email")); assertEquals("Tim", person.get("firstName")); assertEquals("Testerman", person.get("lastName")); }
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <user> <email>[email protected]</email> <firstName>Tim</firstName> <id>1</id> <lastName>Testerman</lastName> </user>
@Test public void testGetSingleUserAsXml() { expect(). statusCode(200). body( "user.email", equalTo("[email protected]"), "user.firstName", equalTo("Tim"), "user.lastName", equalTo("Testerman"), "user.id", equalTo("1")). when(). get("/service/single-user/xml"); }
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <people> <person id="1"> <email>[email protected]</email> <firstName>Tim</firstName> <lastName>Testerman</lastName> </person> <person id="20"> <email>[email protected]</email> <firstName>Sara</firstName> <lastName>Stevens</lastName> </person> <person id="11"> <email>[email protected]</email> <firstName>Mark</firstName> <lastName>Mustache</lastName> </person> </people>
@Test public void testGetPersons() { expect(). statusCode(200). body( hasXPath("//person[@id='1']/email[.='[email protected]'] and firstName='Tim' and lastName='Testerman'"), hasXPath("//person[@id='20']/email[.='[email protected]'] and firstName='Sara' and lastName='Stevens'"), hasXPath("//person[@id='1']/email[.='[email protected]'] and firstName='Mark' and lastName='Mustache'")). when(). get("/service/persons/xml"); }
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <user> <email>[email protected]</email> <firstName>Tim</firstName> <id>1</id> <lastName>Testerman</lastName> </user>
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema"> <element name="user"> <complexType> <sequence> <element name="email"> <simpleType> <restriction base="string"> <pattern value=".+@.+"></pattern> </restriction> </simpleType> </element> <element name="firstName" type="string"></element> <element name="id" type="int"></element> <element name="lastName" type="string"></element> </sequence> </complexType> </element> </schema>
@Test public void testGetSingleUserAgainstSchema() { InputStream xsd = getClass().getResourceAsStream("/user.xsd"); assertNotNull(xsd); expect(). statusCode(200). body( matchesXsd(xsd)). when(). get("/service/single-user/xml"); }
@Test public void testCreateuser() { final String email = "[email protected]"; final String firstName = "Tim"; final String lastName = "Tester"; given(). parameters( "email", email, "firstName", firstName, "lastName", lastName). expect(). body("email", equalTo(email)). body("firstName", equalTo(firstName)). body("lastName", equalTo(lastName)). when(). get("/service/user/create"); }
@Test public void testAuthenticationWorking() { // we're not authenticated, service returns "401 Unauthorized" expect(). statusCode(401). when(). get("/service/secure/person"); // with authentication it is working expect(). statusCode(200). when(). with(). authentication().basic("admin", "admin"). get("/service/secure/person"); }
@Test public void testSetRequestHeaders() { expect(). body(equalTo("TEST")). when(). with(). header("myparam", "TEST"). get("/service/header/print"); expect(). body(equalTo("foo")). when(). with(). header("myparam", "foo"). get("/service/header/print"); }
@Test public void testReturnedHeaders() { expect(). headers("customHeader1", "foo", "anotherHeader", "bar"). when(). get("/service/header/multiple"); }
@Test public void testModifyCookie() { expect(). cookie("userName", equalTo("Ted")). when(). with().param("name", "Ted"). get("/service/cookie/modify"); expect(). cookie("userName", equalTo("Bill")). when(). with().param("name", "Bill"). get("/service/cookie/modify"); }
@Test public void testFileUpload() { final File file = new File(getClass().getClassLoader() .getResource("test.txt").getFile()); assertNotNull(file); assertTrue(file.canRead()); given(). multiPart(file). expect(). body(equalTo("This is an uploaded test file.")). when(). post("/service/file/upload"); }
@Test public void testRegisterParserForUnknownContentType() { RestAssured.registerParser("text/json", Parser.JSON); expect(). body("test", equalTo(true)). when(). get("/service/detail/json"); }
@Test public void testSpecReuse() { ResponseSpecBuilder builder = new ResponseSpecBuilder(); builder.expectStatusCode(200); builder.expectBody("email", equalTo("[email protected]")); builder.expectBody("firstName", equalTo("Tim")); builder.expectBody("lastName", equalTo("Testerman")); builder.expectBody("id", equalTo("1")); ResponseSpecification responseSpec = builder.build(); // now we're able to use this specification for this test expect(). spec(responseSpec). when(). get("/service/single-user"); // now re-use for another test that returns similar data .. you may // extend the specification with further tests as you wish final String email = "[email protected]"; final String firstName = "Tim"; final String lastName = "Testerman"; expect(). spec(responseSpec). when(). with(). parameters( "email", email, "firstName", firstName, "lastName",lastName). get("/service/user/create");