From 5fe4fe2cecc43c9288d7d2ec669b22d4aa0cc61b Mon Sep 17 00:00:00 2001 From: aminecmi Date: Sat, 30 Oct 2021 15:07:05 +0200 Subject: [PATCH] Exception testing. --- .../cartes/CartesApplicationTests.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/test/java/fr/aminelouveaau/cartes/CartesApplicationTests.java b/src/test/java/fr/aminelouveaau/cartes/CartesApplicationTests.java index 8e56d83..9ccef2e 100644 --- a/src/test/java/fr/aminelouveaau/cartes/CartesApplicationTests.java +++ b/src/test/java/fr/aminelouveaau/cartes/CartesApplicationTests.java @@ -2,11 +2,13 @@ package fr.aminelouveaau.cartes; import fr.aminelouveaau.cartes.model.Card; import fr.aminelouveaau.cartes.service.GameService; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.web.client.HttpClientErrorException; import java.util.List; @@ -16,6 +18,8 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat; class CartesApplicationTests { public static final int CARDS_PER_HAND = 10; + public static final int DECK_SIZE = 4 * 13; + @InjectMocks GameService gameService; @@ -29,7 +33,7 @@ class CartesApplicationTests { void testDeckGeneration() { List deck = gameService.getDeck(); assertThat(deck.size()).isNotEqualTo(0).withFailMessage("Deck is empty"); - assertThat(deck.size()).isEqualTo(4 * 13).withFailMessage("Deck is not complete"); + assertThat(deck.size()).isEqualTo(DECK_SIZE).withFailMessage("Deck is not complete"); } @Test @@ -37,8 +41,17 @@ class CartesApplicationTests { List hand = gameService.cardHand(); assertThat(hand.size()).isEqualTo(CARDS_PER_HAND).withFailMessage("Hand is not complete"); List deck = gameService.getDeck(); - assertThat(deck.size()).isNotEqualTo(4 * 13).withFailMessage("Where did the cards come from ?"); - assertThat(deck.size()).isEqualTo((4 * 13) - CARDS_PER_HAND).withFailMessage("Wrong number of cards removed."); + assertThat(deck.size()).isNotEqualTo(DECK_SIZE).withFailMessage("Where did the cards come from ?"); + assertThat(deck.size()).isEqualTo(DECK_SIZE - CARDS_PER_HAND).withFailMessage("Wrong number of cards removed."); + } + + @Test() + void testExceptionTooManyPlayers() { + int maxNumberOfPlayers = (int) Math.floor(DECK_SIZE / CARDS_PER_HAND); + for (int i = 0; i < maxNumberOfPlayers; i++) { + gameService.cardHand(); + } + Assertions.assertThrows(HttpClientErrorException.class, () -> gameService.cardHand()); } @Test