forked from pierront/mylibrary-template
ajout des tests des exceptions
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
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;
|
||||
|
||||
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 = {
|
||||
"Customer ID cannot be null",
|
||||
"Duration is not valid",
|
||||
"Payment Method cannot be blank"
|
||||
})
|
||||
@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 = "Required field is missing";
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,52 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.CustomerNotFoundException;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
||||
public class SubscriptionNotFoundExceptionTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception message should contain the UUID provided")
|
||||
void testExceptionMessageContainsUUID() {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
|
||||
SubscriptionNotFoundException exception = new SubscriptionNotFoundException(uuid);
|
||||
|
||||
String expectedMessage = String.format("The subscription with id %s does not exist", uuid);
|
||||
assertEquals(expectedMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should use the correct constant message format")
|
||||
void testExceptionUsesConstantMessageFormat() {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
|
||||
SubscriptionNotFoundException exception = new SubscriptionNotFoundException(uuid);
|
||||
|
||||
String expectedFormatWithPlaceholder = "The subscription with id {0} does not exist";
|
||||
assertEquals(SubscriptionNotFoundException.THE_SUBSCRIPTION_WITH_ID_DOES_NOT_EXIST_MESSAGE,
|
||||
expectedFormatWithPlaceholder);
|
||||
assertTrue(exception.getMessage().contains(uuid.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should be properly thrown and caught")
|
||||
void testExceptionCanBeThrownAndCaught() {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
|
||||
try {
|
||||
throw new SubscriptionNotFoundException(uuid);
|
||||
} catch (SubscriptionNotFoundException e) {
|
||||
String expectedMessage = String.format("The subscription with id %s does not exist", uuid);
|
||||
assertEquals(expectedMessage, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user