|
| 1 | +package com.baeldung.beanvalidation; |
| 2 | + |
| 3 | +import javax.validation.ConstraintViolation; |
| 4 | +import javax.validation.Validation; |
| 5 | +import javax.validation.Validator; |
| 6 | +import org.junit.AfterClass; |
| 7 | +import org.junit.BeforeClass; |
| 8 | +import org.junit.Test; |
| 9 | +import com.baeldung.beanvalidation.container.IntegerContainer; |
| 10 | +import com.baeldung.beanvalidation.container.StringContainer; |
| 11 | +import com.baeldung.beanvalidation.model.User; |
| 12 | +import com.baeldung.beanvalidation.service.UserService; |
| 13 | +import java.util.Set; |
| 14 | +import java.util.stream.Collectors; |
| 15 | +import static org.junit.Assert.*; |
| 16 | + |
| 17 | +public class ValidationTest { |
| 18 | + |
| 19 | + private static Validator validator; |
| 20 | + private static User user; |
| 21 | + |
| 22 | + @BeforeClass |
| 23 | + public static void setUpHibernateValidatorInstance() { |
| 24 | + validator = Validation.buildDefaultValidatorFactory().getValidator(); |
| 25 | + } |
| 26 | + |
| 27 | + @BeforeClass |
| 28 | + public static void setUpUserInstance() { |
| 29 | + user = new User(); |
| 30 | + } |
| 31 | + |
| 32 | + @AfterClass |
| 33 | + public static void tearDownHibernateValidatorInstance() { |
| 34 | + validator = null; |
| 35 | + } |
| 36 | + |
| 37 | + @AfterClass |
| 38 | + public static void tearDownUserInstance() { |
| 39 | + user = null; |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void givenNullName_whenValidated_thenMessageDescriptorForName() { |
| 44 | + user.setName(null); |
| 45 | + user.setEmail("mary@domain.com"); |
| 46 | + user.setAge(36); |
| 47 | + assertEquals("Name cannot be null", validator.validate(user).stream().map(violation -> violation.getMessage()).collect(Collectors.joining())); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void givenInvalidEmail_whenValidated_thenMessageDescriptorforEmail() { |
| 52 | + user.setName("Mary"); |
| 53 | + user.setEmail("no-email"); |
| 54 | + user.setAge(36); |
| 55 | + assertEquals("Email must be a well-formed email address", validator.validate(user).stream().map(violation -> violation.getMessage()).collect(Collectors.joining())); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void givenAgeLesserThanLowerBound_whenValidated_thenMessageDescriptorforAge() { |
| 60 | + user.setName("Mary"); |
| 61 | + user.setEmail("mary@domain.com"); |
| 62 | + user.setAge(0); |
| 63 | + assertEquals("Age must not be lesser than 1", validator.validate(user).stream().map(violation -> violation.getMessage()).collect(Collectors.joining())); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void givenAgeGreaterThanUpperBound_whenValidated_thenMessageDescriptorforAge() { |
| 68 | + user.setName("Mary"); |
| 69 | + user.setEmail("mary@domain.com"); |
| 70 | + user.setAge(100); |
| 71 | + assertEquals("Age must not be greater than 99", validator.validate(user).stream().map(violation -> violation.getMessage()).collect(Collectors.joining())); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void givenNullFileName_whenValidated_thenMessageDescriptorforFileName() { |
| 76 | + user.setName("Mary"); |
| 77 | + user.setEmail("mary@domain.com"); |
| 78 | + user.setAge(36); |
| 79 | + UserService userService = new UserService(); |
| 80 | + userService.setFileName(null); |
| 81 | + userService.setUser(user); |
| 82 | + assertEquals("FileName cannot be null", validator.validate(userService).stream().map(violation -> violation.getMessage()).collect(Collectors.joining())); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void givenFileNameShortherThanLowerBound_whenValidated_thenMessageDescriptorforFileName() { |
| 87 | + user.setName("Mary"); |
| 88 | + user.setEmail("mary@domain.com"); |
| 89 | + user.setAge(36); |
| 90 | + UserService userService = new UserService(); |
| 91 | + userService.setFileName(""); |
| 92 | + userService.setUser(user); |
| 93 | + assertEquals("FileName must be between 5 and 10 characters", validator.validate(userService).stream().map(violation -> violation.getMessage()).collect(Collectors.joining())); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void givenFileNameLongerThanUpperBound_whenValidated_thenMessageDescriptorforFileName() { |
| 98 | + user.setName("Mary"); |
| 99 | + user.setEmail("mary@domain.com"); |
| 100 | + user.setAge(36); |
| 101 | + UserService userService = new UserService(); |
| 102 | + userService.setFileName("waytoolongfilename"); |
| 103 | + userService.setUser(user); |
| 104 | + assertEquals("FileName must be between 5 and 10 characters", validator.validate(userService).stream().map(violation -> violation.getMessage()).collect(Collectors.joining())); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void givenNullUserAndNullFileName_whenValidated_thenTwoConstraintViolations() { |
| 109 | + user.setName(null); |
| 110 | + user.setEmail("mary@domain.com"); |
| 111 | + user.setAge(36); |
| 112 | + UserService userService = new UserService(); |
| 113 | + userService.setFileName(null); |
| 114 | + userService.setUser(user); |
| 115 | + Set<ConstraintViolation<UserService>> constraintViolations = validator.validate(userService); |
| 116 | + assertEquals(2, constraintViolations.size()); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void givenNullElement_whenValidated_thenOneConstraintViolation() { |
| 121 | + StringContainer container = new StringContainer(); |
| 122 | + container.addElement(null); |
| 123 | + Set<ConstraintViolation<StringContainer>> constraintViolations = validator.validate(container); |
| 124 | + assertEquals(1, constraintViolations.size()); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void givenNegativeInteger_whenValidated_thenOneConstraintViolation() { |
| 129 | + IntegerContainer container = new IntegerContainer(); |
| 130 | + container.addElement(-1); |
| 131 | + Set<ConstraintViolation<IntegerContainer>> constraintViolations = validator.validate(container); |
| 132 | + assertEquals(1, constraintViolations.size()); |
| 133 | + } |
| 134 | +} |
0 commit comments