Exception testing.

This commit is contained in:
aminecmi 2021-10-30 15:07:05 +02:00
parent 987097f7a1
commit 5fe4fe2cec

View File

@ -2,11 +2,13 @@ package fr.aminelouveaau.cartes;
import fr.aminelouveaau.cartes.model.Card; import fr.aminelouveaau.cartes.model.Card;
import fr.aminelouveaau.cartes.service.GameService; import fr.aminelouveaau.cartes.service.GameService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.HttpClientErrorException;
import java.util.List; import java.util.List;
@ -16,6 +18,8 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
class CartesApplicationTests { class CartesApplicationTests {
public static final int CARDS_PER_HAND = 10; public static final int CARDS_PER_HAND = 10;
public static final int DECK_SIZE = 4 * 13;
@InjectMocks @InjectMocks
GameService gameService; GameService gameService;
@ -29,7 +33,7 @@ class CartesApplicationTests {
void testDeckGeneration() { void testDeckGeneration() {
List<Card> deck = gameService.getDeck(); List<Card> deck = gameService.getDeck();
assertThat(deck.size()).isNotEqualTo(0).withFailMessage("Deck is empty"); 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 @Test
@ -37,8 +41,17 @@ class CartesApplicationTests {
List<Card> hand = gameService.cardHand(); List<Card> hand = gameService.cardHand();
assertThat(hand.size()).isEqualTo(CARDS_PER_HAND).withFailMessage("Hand is not complete"); assertThat(hand.size()).isEqualTo(CARDS_PER_HAND).withFailMessage("Hand is not complete");
List<Card> deck = gameService.getDeck(); List<Card> deck = gameService.getDeck();
assertThat(deck.size()).isNotEqualTo(4 * 13).withFailMessage("Where did the cards come from ?"); assertThat(deck.size()).isNotEqualTo(DECK_SIZE).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()).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 @Test