I've have experience in unit testing but I'm fairly new to acceptance testing and REST-assured. I would like to get some feedback on my style. This is not a complete list of all my test but a few that I felt didn't need context. In the code there are two methods that need describing. validHolidayScheme
is a JSON builder that creates a valid holiday scheme with some default data. aHolidayScheme
that is JSON builder that creates a holiday scheme with some name specified without any other defaults.
When doing unit testing I use the AAA pattern but it feels kind of silly adding comments like //Arrange
, //Act
and //Assert
when you already have given()
, when()
, then()
inside the tests. How would you use the AAA pattern with REST-assured tests or how would you structure your code?
Also, my tests have a lot of code duplication but still simple and I don't want to extract to much into helper methods making the tests obscure. Would you, and if so, what would you extract into helper methods?
Any other suggestions on how I could improve my code?
public class HolidaySchemeTest {
@Test
public void shouldAddSchemeToList() {
int schemeId = given().body(validHolidayScheme())
.post("/rest/holidayscheme").jsonPath().getInt("id");
given().get("/rest/holidayscheme")
.then()
.statusCode(Response.Status.OK.getStatusCode())
.body("id", hasItem(schemeId));
}
@Test
public void shouldGetHolidayScheme() {
int schemeId = given().body(aHolidayScheme("The holiday"))
.post("/rest/holidayscheme").jsonPath().getInt("id");
given().get("/rest/holidayscheme/" + schemeId)
.then()
.statusCode(Response.Status.OK.getStatusCode())
.body("id", equalTo(schemeId))
.body("name", equalTo("The holiday"));
}
@Test
public void shouldUpdateHolidayScheme() {
int schemeId = given().body(validHolidayScheme().withName("my holidays"))
.post("/rest/holidayscheme").jsonPath().getInt("id");
given().body(validHolidayScheme().withName("alt holidays").withId(schemeId))
.put("/rest/holidayscheme/" + schemeId)
.then()
.statusCode(Response.Status.OK.getStatusCode());
given().get("/rest/holidayscheme/" + schemeId)
.then()
.statusCode(Response.Status.OK.getStatusCode())
.body("name", equalTo("alt holidays"));
}
@Test
public void shouldDeleteHolidayScheme() {
int schemeId = given().body(validHolidayScheme().withName("my holidays"))
.post("/rest/holidayscheme").jsonPath().getInt("id");
given().delete("/rest/holidayscheme/" + schemeId)
.then()
.statusCode(Response.Status.OK.getStatusCode());
given().get("/rest/holidayscheme/" + schemeId)
.then().statusCode(Response.Status.NOT_FOUND.getStatusCode());
}
@Test
public void shouldUpdateFixedHolidayForHolidayScheme() {
//Arrange
int schemeId = given().body(validHolidayScheme())
.post("/rest/holidayscheme").jsonPath().getInt("id");
int holidayId = given().body(validHoliday())
.post("/rest/holidayscheme/" + schemeId + "/days/fixed")
.jsonPath().getInt("id");
//Act
given().body(aHoliday().withName("Xmas").withDate("25/Dec").withDuration("3h"))
.put("/rest/holidayscheme/" + schemeId + "/days/fixed/" + holidayId)
.then()
.statusCode(Response.Status.OK.getStatusCode());
//Assert
given().get("/rest/holidayscheme/" + schemeId + "/days/fixed/" + holidayId)
.then()
.statusCode(Response.Status.OK.getStatusCode())
.body("name", equalTo("Xmas"))
.body("date", equalTo("25/Dec"))
.body("duration", equalTo("3h"));
}
@Test
public void shouldDeleteHolidayFromHolidayScheme() {
//Arrange
int schemeId = given().body(validHolidayScheme())
.post("/rest/holidayscheme").jsonPath().getInt("id");
int holidayId = given().body(validHoliday())
.post("/rest/holidayscheme/" + schemeId + "/days/fixed").jsonPath().getInt("id");
//Act
given().delete("/rest/holidayscheme/" + schemeId + "/days/fixed/" + holidayId)
.then()
.statusCode(Response.Status.OK.getStatusCode());
//Assert
given().get("/rest/holidayscheme/" + schemeId + "/days/fixed")
.then()
.statusCode(Response.Status.OK.getStatusCode())
.body("", hasSize(0));
}
@Test
public void shouldAssignMemberToHolidayScheme() {
int schemeId = given().body(validHolidayScheme())
.post("/rest/holidayscheme").jsonPath().getInt("id");
given().put("/rest/holidayscheme/" + schemeId + "/member/admin")
.then()
.statusCode(Response.Status.OK.getStatusCode());
given().get("/rest/holidayscheme/" + schemeId + "/members")
.then()
.statusCode(Response.Status.OK.getStatusCode())
.body("username", hasItems("admin"));
}
@Test
public void shouldNotDeleteHolidaySchemeWithMember() {
int schemeId = given().body(validHolidayScheme())
.post("/rest/holidayscheme").jsonPath().getInt("id");
given().put("/rest/holidayscheme/" + schemeId + "/member/admin")
.then()
.statusCode(Response.Status.OK.getStatusCode());
given().delete("/rest/holidayscheme/" + schemeId)
.then() //Assert
.statusCode(Response.Status.BAD_REQUEST.getStatusCode());
}
@Test
public void shouldCountMembersOfHolidayScheme() {
//Arrange
int schemeId = given().body(validHolidayScheme())
.post("/rest/holidayscheme").jsonPath().getInt("id");
given().put("/rest/holidayscheme/" + schemeId + "/member/adam")
.then()
.statusCode(Response.Status.OK.getStatusCode());
given().put("/rest/holidayscheme/" + schemeId + "/member/eve")
.then()
.statusCode(Response.Status.OK.getStatusCode());
//Act
given().get("/rest/holidayscheme")
.then() //Assert
.body("count", contains(2));
}
@Test
public void shouldCountMembersOfHolidaySchemeWhenMemberIsMoved() {
//Arrange
int firstSchemeId = given().body(validHolidayScheme())
.post("/rest/holidayscheme").jsonPath().getInt("id");
given().put("/rest/holidayscheme/" + firstSchemeId + "/member/adam")
.then()
.statusCode(Response.Status.OK.getStatusCode());
given().put("/rest/holidayscheme/" + firstSchemeId + "/member/eve")
.then()
.statusCode(Response.Status.OK.getStatusCode());
int secondSchemeId = given().body(validHolidayScheme())
.post("/rest/holidayscheme").jsonPath().getInt("id");
given().put("/rest/holidayscheme/" + secondSchemeId + "/member/eve");
//Act
given().get("/rest/holidayscheme")
.then() //Assert
.statusCode(Response.Status.OK.getStatusCode())
.body("count", contains(1, 1));
}
}