Reussite du test de reduction des stock avec l'exception possible

This commit is contained in:
2026-03-25 12:24:46 +01:00
parent 961b3b69ba
commit 2b29d34615
3 changed files with 86 additions and 3 deletions
@@ -1,7 +1,7 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.entity;
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.book.converter.BookConverter;
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.IllegalBookStockException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
@@ -9,7 +9,8 @@ import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class BookTest {
@@ -84,5 +85,66 @@ public class BookTest {
assertEquals(copyExpected, book.getStock());
}
@Test
@DisplayName("removeLoyaltyPoints should correctly decrement loyalty points")
void testRemoveCopy() throws IllegalBookStockException {
Book book =Book.builder()
.stock(5)
.build();
Integer copyToRemove = 2;
Integer copyExpected = 3;
book.removeStock(copyToRemove);
assertEquals(copyExpected, book.getStock());
}
@Test
@DisplayName("removeLoyaltyPoints should correctly decrement loyalty points")
void testRemoveZeroToCopy() throws IllegalBookStockException {
Book book =Book.builder()
.stock(5)
.build();
Integer copyToRemove = 0;
Integer copyExpected = 5;
book.removeStock(copyToRemove);
assertEquals(copyExpected, book.getStock());
}
@Test
@DisplayName("removeLoyaltyPoints should correctly decrement loyalty points")
void testRemoveAllToCopy() throws IllegalBookStockException {
Book book =Book.builder()
.stock(5)
.build();
Integer copyToRemove = 5;
Integer copyExpected = 0;
book.removeStock(copyToRemove);
assertEquals(copyExpected, book.getStock());
}
@Test
@DisplayName("removeSTock should throw exception when trying to remove more copy than available")
void testRemoveTooManyCopy() {
Book book = Book.builder()
.stock(50)
.build();
int copyToRemove = 75;
IllegalBookStockException exception = assertThrows(
IllegalBookStockException.class,
() -> book.removeStock(copyToRemove)
);
assertEquals(50, book.getStock());
assertTrue(exception.getMessage().contains(String.valueOf(copyToRemove)));
assertTrue(exception.getMessage().contains(String.valueOf(book.getStock())));
}
}
}