|
16 | 16 | public class ValidationTest { |
17 | 17 |
|
18 | 18 | @Test |
19 | | - public void validation_when_fname_is_null() { |
20 | | - System.out |
21 | | - .println("\n---------------validation_when_fname_is_null--------------"); |
22 | | - |
23 | | - // [1] |
| 19 | + public void ifNameIsNull_nameValidationFails() { |
24 | 20 | User user = new User(); |
25 | | - user.setLname("Last"); |
26 | | - user.setEmail("first.last@gmail.com"); |
27 | | - user.setGender("male"); |
28 | | - user.setAge(29); |
29 | | - |
30 | | - // [2] |
31 | | - ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); |
32 | | - // [3] |
| 21 | + user.setWorking(true); |
| 22 | + user.setAboutMe("Its all about me!!"); |
| 23 | + user.setAge(50); |
| 24 | + |
| 25 | + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); |
33 | 26 | Validator validator = factory.getValidator(); |
34 | | - // [4] |
35 | 27 | Set<ConstraintViolation<User>> violations = validator.validate(user); |
36 | | - |
37 | | - // [5] |
38 | 28 | Iterator<ConstraintViolation<User>> iter = violations.iterator(); |
39 | 29 |
|
40 | 30 | while (iter.hasNext()) { |
41 | | - // [6] |
42 | 31 | ConstraintViolation<User> cv = iter.next(); |
43 | | - |
44 | | - // [7] |
45 | | - System.out.println(cv.getMessage()); |
46 | 32 | } |
47 | 33 |
|
48 | 34 | Assert.assertEquals(violations.isEmpty(), false); |
49 | 35 | } |
50 | 36 |
|
51 | 37 | @Test |
52 | | - public void validation_when_empty_lname() { |
53 | | - System.out |
54 | | - .println("\n---------------validation_when_empty_lname--------------"); |
| 38 | + public void ifWorkingIsFalse_workingValidationFails() { |
55 | 39 | User user = new User(); |
56 | | - user.setFname("First"); |
57 | | - user.setLname(""); |
58 | | - user.setEmail("first.last@gmail.com"); |
59 | | - user.setGender("male"); |
60 | | - user.setAge(29); |
61 | | - |
62 | | - // validate the input |
63 | | - ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); |
| 40 | + user.setName("MyName"); |
| 41 | + user.setAboutMe("Its all about me!!"); |
| 42 | + user.setAge(50); |
| 43 | + |
| 44 | + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); |
64 | 45 | Validator validator = factory.getValidator(); |
65 | 46 | Set<ConstraintViolation<User>> violations = validator.validate(user); |
66 | | - |
67 | 47 | Iterator<ConstraintViolation<User>> iter = violations.iterator(); |
68 | 48 |
|
69 | 49 | while (iter.hasNext()) { |
70 | 50 | ConstraintViolation<User> cv = iter.next(); |
71 | | - System.out.println(cv.getMessage()); |
72 | 51 | } |
73 | 52 |
|
74 | 53 | Assert.assertEquals(violations.isEmpty(), false); |
75 | 54 | } |
76 | 55 |
|
77 | 56 | @Test |
78 | | - public void validation_when_email_is_invalid() { |
79 | | - System.out |
80 | | - .println("\n---------------validation_when_email_is_invalid--------------"); |
| 57 | + public void ifAgeNotRange_ageValidationFails() { |
81 | 58 | User user = new User(); |
82 | | - user.setFname("First"); |
83 | | - user.setLname("Last"); |
84 | | - user.setEmail("firstgmail.com"); |
85 | | - user.setGender("male"); |
86 | | - user.setAge(29); |
87 | | - |
88 | | - // validate the input |
89 | | - ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); |
| 59 | + user.setName("MyName"); |
| 60 | + user.setAboutMe("Its all about me!!"); |
| 61 | + user.setAge(8); |
| 62 | + |
| 63 | + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); |
90 | 64 | Validator validator = factory.getValidator(); |
91 | 65 | Set<ConstraintViolation<User>> violations = validator.validate(user); |
92 | | - |
93 | 66 | Iterator<ConstraintViolation<User>> iter = violations.iterator(); |
94 | 67 |
|
95 | 68 | while (iter.hasNext()) { |
96 | 69 | ConstraintViolation<User> cv = iter.next(); |
97 | | - System.out.println(cv.getMessage()); |
98 | 70 | } |
99 | 71 |
|
100 | 72 | Assert.assertEquals(violations.isEmpty(), false); |
|
0 commit comments