Bezugnehmend auf https://www.java-forum.org/thema/spring-entity-testen.197377/
Habe ich mein TestCase jetzt noch etwas erweitert:
Der Test testFirstnameWith100CharsMustBeValid() schlägt aber fehl. Was ich nicht so ganz verstehe, denn firstname ist annotiert mit
@Size(max=100)
Habe ich mein TestCase jetzt noch etwas erweitert:
Java:
import static org.junit.jupiter.api.Assertions.*;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
/**
* test class for the user entity
*/
@SpringBootTest
class UserTest {
private static Validator validator;
/**
* initialize the Validation for testing
*/
@BeforeAll
public static void init() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
}
private String generateNChars(int length) {
return new String(new char[length]).replace('\0', ' ');
}
/**
* test the class with valid datas
*/
@Test
void testCorrectDatasMustBeValidate() {
User user = new User();
user.setFirstname("aaaaa");
user.setLastname("bbbb");
user.setEmail("email@email.com");
user.setPassword("a1b2c3");
Set<ConstraintViolation<User>> violations = validator.validate(user);
assertTrue(violations.isEmpty());
}
/**
* test the class with empty firstname
*/
@Test
void testFirstnameWithEmptyStringMustBeInvalid() {
User user = new User();
user.setFirstname("");
user.setLastname("bbbb");
user.setEmail("email@email.com");
user.setPassword("a1b2c3");
Set<ConstraintViolation<User>> violations = validator.validate(user);
assertFalse(violations.isEmpty());
}
/**
* test the class with blank firstname
*/
@Test
void testFirstnameWithBlankStringMustBeInvalid() {
User user = new User();
user.setFirstname(" ");
user.setLastname("bbbb");
user.setEmail("email@email.com");
user.setPassword("a1b2c3");
Set<ConstraintViolation<User>> violations = validator.validate(user);
assertFalse(violations.isEmpty());
}
/**
* test the class with 100 chars for firstname must be valid
*/
@Test
void testFirstnameWith100CharsMustBeValid() {
User user = new User();
int length = 100;
String firstname = this.generateNChars(length);
assertTrue(firstname.length() == length);
user.setFirstname( firstname );
user.setLastname("bbbb");
user.setEmail("email@email.com");
user.setPassword("a1b2c3");
Set<ConstraintViolation<User>> violations = validator.validate(user);
assertTrue(violations.isEmpty());
}
/**
* test the class with 101 chars for firstname must be invalid
*/
@Test
void testFirstnameWith101CharsMustBeInvalid() {
User user = new User();
int length = 101;
String firstname = this.generateNChars(length);
assertTrue(firstname.length() == length);
user.setFirstname( firstname );
user.setLastname("bbbb");
user.setEmail("email@email.com");
user.setPassword("a1b2c3");
Set<ConstraintViolation<User>> violations = validator.validate(user);
assertFalse(violations.isEmpty());
}
}
Der Test testFirstnameWith100CharsMustBeValid() schlägt aber fehl. Was ich nicht so ganz verstehe, denn firstname ist annotiert mit
@Size(max=100)