forked from pierront/mylibrary-template
validtest
This commit is contained in:
+61
@@ -0,0 +1,61 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.avis.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.*;
|
||||||
|
|
||||||
|
class NotValidAvisExceptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be created with the provided message")
|
||||||
|
void testExceptionCreation() {
|
||||||
|
String errorMessage = "Note must be between 1 and 5";
|
||||||
|
|
||||||
|
NotValidAvisException exception = new NotValidAvisException(errorMessage);
|
||||||
|
|
||||||
|
assertEquals(errorMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {
|
||||||
|
"Note must be between 1 and 5",
|
||||||
|
"Commentaire cannot be blank",
|
||||||
|
"Client id cannot be null",
|
||||||
|
"Livre id cannot be null",
|
||||||
|
"Date achat cannot be null"
|
||||||
|
})
|
||||||
|
@DisplayName("Exception should handle different validation messages")
|
||||||
|
void testExceptionWithDifferentMessages(String errorMessage) {
|
||||||
|
NotValidAvisException exception = new NotValidAvisException(errorMessage);
|
||||||
|
|
||||||
|
assertEquals(errorMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be properly thrown and caught")
|
||||||
|
void testExceptionCanBeThrownAndCaught() {
|
||||||
|
String errorMessage = "Note must be between 1 and 5";
|
||||||
|
|
||||||
|
Exception exception = assertThrows(NotValidAvisException.class, () -> {
|
||||||
|
throw new NotValidAvisException(errorMessage);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals(errorMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be catchable as a general Exception")
|
||||||
|
void testExceptionInheritance() {
|
||||||
|
String errorMessage = "Commentaire cannot be blank";
|
||||||
|
|
||||||
|
try {
|
||||||
|
throw new NotValidAvisException(errorMessage);
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertEquals(NotValidAvisException.class, e.getClass());
|
||||||
|
assertEquals(errorMessage, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user