|
23 | 23 |
|
24 | 24 | package com.iluwatar.monad;
|
25 | 25 |
|
26 |
| -import org.junit.jupiter.api.Test; |
27 |
| - |
28 |
| -import java.util.Objects; |
29 |
| - |
30 | 26 | import static org.junit.jupiter.api.Assertions.assertSame;
|
31 | 27 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
32 | 28 |
|
| 29 | +import java.util.Objects; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
33 | 32 | /**
|
34 | 33 | * Test for Monad Pattern
|
35 | 34 | */
|
36 | 35 | public class MonadTest {
|
37 | 36 |
|
38 | 37 | @Test
|
39 | 38 | public void testForInvalidName() {
|
40 |
| - User tom = new User(null, 21, Sex.MALE, "tom@foo.bar"); |
41 |
| - assertThrows(IllegalStateException.class, () -> { |
42 |
| - Validator.of(tom).validate(User::getName, Objects::nonNull, "name cannot be null").get(); |
43 |
| - }); |
| 39 | + var tom = new User(null, 21, Sex.MALE, "tom@foo.bar"); |
| 40 | + assertThrows( |
| 41 | + IllegalStateException.class, |
| 42 | + () -> Validator.of(tom) |
| 43 | + .validate(User::getName, Objects::nonNull, "name cannot be null") |
| 44 | + .get() |
| 45 | + ); |
44 | 46 | }
|
45 | 47 |
|
46 | 48 | @Test
|
47 | 49 | public void testForInvalidAge() {
|
48 |
| - User john = new User("John", 17, Sex.MALE, "john@qwe.bar"); |
49 |
| - assertThrows(IllegalStateException.class, () -> { |
50 |
| - Validator.of(john).validate(User::getName, Objects::nonNull, "name cannot be null") |
51 |
| - .validate(User::getAge, age -> age > 21, "user is underaged") |
52 |
| - .get(); |
53 |
| - }); |
| 50 | + var john = new User("John", 17, Sex.MALE, "john@qwe.bar"); |
| 51 | + assertThrows( |
| 52 | + IllegalStateException.class, |
| 53 | + () -> Validator.of(john) |
| 54 | + .validate(User::getName, Objects::nonNull, "name cannot be null") |
| 55 | + .validate(User::getAge, age -> age > 21, "user is underage") |
| 56 | + .get() |
| 57 | + ); |
54 | 58 | }
|
55 | 59 |
|
56 | 60 | @Test
|
57 | 61 | public void testForValid() {
|
58 |
| - User sarah = new User("Sarah", 42, Sex.FEMALE, "sarah@det.org"); |
59 |
| - User validated = Validator.of(sarah).validate(User::getName, Objects::nonNull, "name cannot be null") |
60 |
| - .validate(User::getAge, age -> age > 21, "user is underaged") |
| 62 | + var sarah = new User("Sarah", 42, Sex.FEMALE, "sarah@det.org"); |
| 63 | + var validated = Validator.of(sarah) |
| 64 | + .validate(User::getName, Objects::nonNull, "name cannot be null") |
| 65 | + .validate(User::getAge, age -> age > 21, "user is underage") |
61 | 66 | .validate(User::getSex, sex -> sex == Sex.FEMALE, "user is not female")
|
62 | 67 | .validate(User::getEmail, email -> email.contains("@"), "email does not contain @ sign")
|
63 | 68 | .get();
|
|
0 commit comments