HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Java REST-assured acceptance tests

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
restacceptancetestsjavaassured

Problem

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 = giv

Solution


  • Yeah I'd leave out the comments, the code should be self-evident.



  • Any way to get the paths from somewhere instead of hardcoding them?


You could also extract them into constants if there's no good way
and/or extract methods to fill in values.

  • Possibly extract other constant patterns (...getStatusCode) to


reduce the number of tokens to read.

Context

StackExchange Code Review Q#147244, answer score: 2

Revisions (0)

No revisions yet.