From 9e69e020669c270581111b942ab8454682608afb Mon Sep 17 00:00:00 2001 From: aubert Date: Sun, 14 Jun 2026 00:00:18 +0200 Subject: [PATCH] =?UTF-8?q?:white=5Fcheck=5Fmark:=20cr=C3=A9ation=20des=20?= =?UTF-8?q?tests=20d'exception'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NotValidPaymentMethodEsceptionTest.java | 58 ++++++++++++++++++ .../NotValidSubscriptionExceptionTest.java | 60 +++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/subscription/exception/NotValidPaymentMethodEsceptionTest.java create mode 100644 src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/subscription/exception/NotValidSubscriptionExceptionTest.java diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/subscription/exception/NotValidPaymentMethodEsceptionTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/subscription/exception/NotValidPaymentMethodEsceptionTest.java new file mode 100644 index 0000000..7ae5af4 --- /dev/null +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/subscription/exception/NotValidPaymentMethodEsceptionTest.java @@ -0,0 +1,58 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class NotValidPaymentMethodEsceptionTest { + + @Test + @DisplayName("Exception should be created with the provided message") + void testExceptionCreation() { + String errorMessage = "PaymentMethod data is not valid"; + + NotValidPaymentMethodException exception = new NotValidPaymentMethodException(errorMessage); + + assertEquals(errorMessage, exception.getMessage()); + } + + @ParameterizedTest + @ValueSource(strings = { + "paymentType is not valid" + }) + @DisplayName("Exception should handle different validation messages") + void testExceptionWithDifferentMessages(String errorMessage) { + NotValidPaymentMethodException exception = new NotValidPaymentMethodException(errorMessage); + + assertEquals(errorMessage, exception.getMessage()); + } + + @Test + @DisplayName("Exception should be properly thrown and caught") + void testExceptionCanBeThrownAndCaught() { + String errorMessage = "paymentType is not valid"; + + Exception exception = assertThrows(NotValidPaymentMethodException.class, () -> { + throw new NotValidPaymentMethodException(errorMessage); + }); + + assertEquals(errorMessage, exception.getMessage()); + } + + @Test + @DisplayName("Exception should be catchable as a general Exception") + void testExceptionInheritance() { + String errorMessage = "Invalid paymentMethod data"; + + try { + throw new NotValidPaymentMethodException(errorMessage); + } catch (Exception e) { + assertEquals(NotValidPaymentMethodException.class, e.getClass()); + assertEquals(errorMessage, e.getMessage()); + } + } +} diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/subscription/exception/NotValidSubscriptionExceptionTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/subscription/exception/NotValidSubscriptionExceptionTest.java new file mode 100644 index 0000000..a04d067 --- /dev/null +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/subscription/exception/NotValidSubscriptionExceptionTest.java @@ -0,0 +1,60 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception; + +import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.NotValidCustomerException; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class NotValidSubscriptionExceptionTest { + + @Test + @DisplayName("Exception should be created with the provided message") + void testExceptionCreation() { + String errorMessage = "Subscription data is not valid"; + + NotValidSubscriptionException exception = new NotValidSubscriptionException(errorMessage); + + assertEquals(errorMessage, exception.getMessage()); + } + + @ParameterizedTest + @ValueSource(strings = { + "subscriptionDurationDesired will be 3, 6 or 12 by the ENUM file SubscriptionDurationDesired", + "desiredStartDate cannot be before today" + }) + @DisplayName("Exception should handle different validation messages") + void testExceptionWithDifferentMessages(String errorMessage) { + NotValidSubscriptionException exception = new NotValidSubscriptionException(errorMessage); + + assertEquals(errorMessage, exception.getMessage()); + } + + @Test + @DisplayName("Exception should be properly thrown and caught") + void testExceptionCanBeThrownAndCaught() { + String errorMessage = "desiredStartDate cannot be before today"; + + Exception exception = assertThrows(NotValidSubscriptionException.class, () -> { + throw new NotValidSubscriptionException(errorMessage); + }); + + assertEquals(errorMessage, exception.getMessage()); + } + + @Test + @DisplayName("Exception should be catchable as a general Exception") + void testExceptionInheritance() { + String errorMessage = "Invalid subscription data"; + + try { + throw new NotValidSubscriptionException(errorMessage); + } catch (Exception e) { + assertEquals(NotValidSubscriptionException.class, e.getClass()); + assertEquals(errorMessage, e.getMessage()); + } + } +}