Validations-Test mit @Size

MIVA

Mitglied
Bezugnehmend auf https://www.java-forum.org/thema/spring-entity-testen.197377/

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)
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
R Spring Boot Test Assertions mit Objekten Frameworks - Spring, Play, Blade, Vaadin & Co 6
8u3631984 Pfad zu Test Datei in application.yml in Spring Boot Test Frameworks - Spring, Play, Blade, Vaadin & Co 7
D Spring Boot Test ob Validation geprüft wurde Frameworks - Spring, Play, Blade, Vaadin & Co 8
8u3631984 Beim WebMVC Test wird Resource File nicht gefunden. Frameworks - Spring, Play, Blade, Vaadin & Co 1
T Repository wird null im Test Frameworks - Spring, Play, Blade, Vaadin & Co 6
R Wie schreibt man Test für einen Controller? Frameworks - Spring, Play, Blade, Vaadin & Co 5
D SpringBootTest H2 Database für Test Frameworks - Spring, Play, Blade, Vaadin & Co 5
8u3631984 Test Properties aus application.yml Frameworks - Spring, Play, Blade, Vaadin & Co 1
D Spring Unit Test: UnsatisfiedDependencyException: Error creating bean with name Frameworks - Spring, Play, Blade, Vaadin & Co 2
8u3631984 Bestpractice : Test JPA Repositories Frameworks - Spring, Play, Blade, Vaadin & Co 9
8u3631984 Autowired App im Test später starten Frameworks - Spring, Play, Blade, Vaadin & Co 0
8u3631984 Spring JPA Test in Gradle Sub Module Frameworks - Spring, Play, Blade, Vaadin & Co 1
T JUnit parametrisierter Test Frameworks - Spring, Play, Blade, Vaadin & Co 4
8u3631984 Spring Test findet Component nicht Frameworks - Spring, Play, Blade, Vaadin & Co 4
OnDemand Spring Boot Test mit Junit Frameworks - Spring, Play, Blade, Vaadin & Co 8
bueseb84 Unit Test mit Spring Boot - Service Autowired Frameworks - Spring, Play, Blade, Vaadin & Co 5
F Spring Boot Test Frameworks - Spring, Play, Blade, Vaadin & Co 14
S Integrations Test in Java mit Spring Frameworks - Spring, Play, Blade, Vaadin & Co 2
J Test mit Hibernate und Spring Frameworks - Spring, Play, Blade, Vaadin & Co 5
P Transaktionaler Spring Test ohne von AbstractTransactionalJUnit4SpringContextTests zu erben Frameworks - Spring, Play, Blade, Vaadin & Co 4

Ähnliche Java Themen

Neue Themen


Oben