diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundException.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundException.java new file mode 100644 index 0000000..51136c0 --- /dev/null +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundException.java @@ -0,0 +1,11 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.book.exception; + +import java.text.MessageFormat; + +public class BookNotFoundException extends RuntimeException { + public static final String THE_BOOK_WITH_ID_DOES_NOT_EXIST_MESSAGE = "The book with isbn {0} does not exist"; + + public BookNotFoundException(String isbn) { + super(MessageFormat.format(THE_BOOK_WITH_ID_DOES_NOT_EXIST_MESSAGE, isbn)); + } +} diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundExceptionTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundExceptionTest.java new file mode 100644 index 0000000..bbcc134 --- /dev/null +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundExceptionTest.java @@ -0,0 +1,48 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.book.exception; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class BookNotFoundExceptionTest { + + @Test + @DisplayName("Exception message should contain the isbn provided") + void testExceptionMessageContainsIsbn() { + String isbn = "1234567891012"; + + BookNotFoundException exception = new BookNotFoundException(isbn); + + String expectedMessage = String.format("The book with isbn %s does not exist", isbn); + assertEquals(expectedMessage, exception.getMessage()); + } + + @Test + @DisplayName("Exception should use the correct constant message format") + void testExceptionUsesConstantMessageFormat() { + String isbn = "1234567891012"; + + BookNotFoundException exception = new BookNotFoundException(isbn); + + String expectedFormatWithPlaceholder = "The book with isbn {0} does not exist"; + assertEquals(BookNotFoundException.THE_BOOK_WITH_ID_DOES_NOT_EXIST_MESSAGE, + expectedFormatWithPlaceholder); + assertTrue(exception.getMessage().contains(isbn.toString())); + } + + @Test + @DisplayName("Exception should be properly thrown and caught") + void testExceptionCanBeThrownAndCaught() { + String isbn = "1234567891012"; + + try { + throw new BookNotFoundException(isbn); + } catch (BookNotFoundException e) { + String expectedMessage = String.format("The book with isbn %s does not exist", isbn); + assertEquals(expectedMessage, e.getMessage()); + } + } +} diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/usecase/BookUseCaseTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/usecase/BookUseCaseTest.java index 64f1eec..eda3b8c 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/usecase/BookUseCaseTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/usecase/BookUseCaseTest.java @@ -6,6 +6,7 @@ import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo; import fr.iut_fbleau.but3.dev62.mylibrary.book.BookSalesInfo; import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book; import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.BookNotFoundException; +import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.BookNotFoundExceptionTest; import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.IllegalBookStockException; import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException; import fr.iut_fbleau.but3.dev62.mylibrary.book.repository.BookRepository;