forked from pierront/mylibrary-template
✅ Reussite du test de reduction des stock avec l'exception possible
This commit is contained in:
@@ -55,7 +55,7 @@ public class BookTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
@DisplayName("Loyalty Points Tests")
|
@DisplayName("Stock Tests")
|
||||||
class StockTests {
|
class StockTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -147,4 +147,22 @@ public class BookTest {
|
|||||||
assertTrue(exception.getMessage().contains(String.valueOf(book.getStock())));
|
assertTrue(exception.getMessage().contains(String.valueOf(book.getStock())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Price Tests")
|
||||||
|
class PriceTests {
|
||||||
|
@Test
|
||||||
|
@DisplayName("addCopy should correctly increment stocks")
|
||||||
|
void testAddCopy() {
|
||||||
|
Book book =Book.builder()
|
||||||
|
.stock(5)
|
||||||
|
.build();
|
||||||
|
Integer copyToAdd = 5;
|
||||||
|
Integer copyExpected = 10;
|
||||||
|
|
||||||
|
book.addStock(copyToAdd);
|
||||||
|
|
||||||
|
assertEquals(copyExpected, book.getStock());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+71
@@ -0,0 +1,71 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.IllegalCustomerPointException;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.CsvSource;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class IllegalBookStockExceptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception message should contain the needed and actual stock")
|
||||||
|
void testExceptionMessageContainsStock() {
|
||||||
|
int neededStock = 100;
|
||||||
|
int actualStock = 50;
|
||||||
|
|
||||||
|
IllegalBookStockException exception = new IllegalBookStockException(neededStock, actualStock);
|
||||||
|
|
||||||
|
String expectedMessage = "Cannot remove 100 copy from 50 copy";
|
||||||
|
assertEquals(expectedMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@CsvSource({
|
||||||
|
"100, 50",
|
||||||
|
"75, 25",
|
||||||
|
"200, 150",
|
||||||
|
"1000, 750"
|
||||||
|
})
|
||||||
|
@DisplayName("Exception message should be formatted correctly for different stock values")
|
||||||
|
void testExceptionMessageForDifferentStockValues(int neededStock, int actualStock) {
|
||||||
|
IllegalBookStockException exception = new IllegalBookStockException(neededStock, actualStock);
|
||||||
|
|
||||||
|
String expectedMessage = MessageFormat.format(IllegalBookStockException.CANNOT_REMOVE_COPY, neededStock, actualStock);
|
||||||
|
assertEquals(expectedMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should use the correct constant message format")
|
||||||
|
void testExceptionUsesConstantMessageFormat() {
|
||||||
|
int neededStock = 100;
|
||||||
|
int actualStock = 50;
|
||||||
|
|
||||||
|
IllegalBookStockException exception = new IllegalBookStockException(neededStock, actualStock);
|
||||||
|
|
||||||
|
String expectedFormatWithPlaceholder = "Cannot remove {0} copy from {1} copy";
|
||||||
|
assertEquals(IllegalBookStockException.CANNOT_REMOVE_COPY,
|
||||||
|
expectedFormatWithPlaceholder);
|
||||||
|
assertTrue(exception.getMessage().contains(String.valueOf(neededStock)));
|
||||||
|
assertTrue(exception.getMessage().contains(String.valueOf(actualStock)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be properly thrown and caught")
|
||||||
|
void testExceptionCanBeThrownAndCaught() {
|
||||||
|
int neededStock = 100;
|
||||||
|
int actualStock = 50;
|
||||||
|
|
||||||
|
try {
|
||||||
|
throw new IllegalBookStockException(neededStock, actualStock);
|
||||||
|
} catch (IllegalBookStockException e) {
|
||||||
|
String expectedMessage = String.format("Cannot remove %d copy from %d copy", neededStock, actualStock);
|
||||||
|
assertEquals(expectedMessage, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user