Search⌘ K
AI Features

Managing Game Start Errors in Wordz

Explore techniques for managing game start errors in a Wordz microservice by test-driving appropriate HTTP responses like 409 Conflict. Understand how to maintain test isolation when running endpoint tests and apply best practices to ensure reliable, readable, and concise code.

Handling errors when starting a game

One of our design decisions is that a player cannot start a game when one is in progress. We need to test-drive this behavior. We choose to return an HTTP status of 409 Conflict to indicate that a game is already in progress for a player and a new one cannot be started for them.

  • We write the test to return a 409 Conflict if the game is already in progress:

Java
@Test
void rejectsRestart() throws Exception {
when(mockWordz.newGame(eq(player))).thenReturn(false);
var req = requestBuilder("start").POST(asJsonBody(player)).build();
var res = httpClient.send(req, HttpResponse.BodyHandlers.discarding());
assertThat(res).hasStatusCode(HttpStatus.CONFLICT.code);
}
...