From ee299e1e19eb957b55a9873a77303bc630dd4239 Mon Sep 17 00:00:00 2001 From: felix-vi Date: Tue, 28 Apr 2026 19:09:55 +0200 Subject: [PATCH] =?UTF-8?q?:white=5Fcheck=5Fmark:=20r=C3=A9ussite=20des=20?= =?UTF-8?q?test=20du=20fichier=20exception=20BookNotFoundException=20et=20?= =?UTF-8?q?bons=20appel=20dans=20les=20test=20des=20usecases?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../book/exception/BookNotFoundException.java | 11 +++++ .../exception/BookNotFoundExceptionTest.java | 48 +++++++++++++++++++ .../book/usecase/BookUseCaseTest.java | 1 + 3 files changed, 60 insertions(+) create mode 100644 src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundException.java create mode 100644 src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundExceptionTest.java 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;