21
21
22
22
import jakarta .validation .Valid ;
23
23
import jakarta .validation .constraints .Max ;
24
+ import jakarta .validation .constraints .NotEmpty ;
24
25
import jakarta .validation .constraints .Size ;
25
26
import org .junit .jupiter .api .Test ;
26
27
@@ -38,45 +39,45 @@ public class HandlerMethodTests {
38
39
@ Test
39
40
void shouldValidateArgsWithConstraintsDirectlyOnClass () {
40
41
Object target = new MyClass ();
41
- testShouldValidateArguments (target , List .of ("addIntValue" , "addPersonAndIntValue" , "addPersons" ), true );
42
- testShouldValidateArguments (target , List .of ("addPerson" , "getPerson" , "getIntValue" , "addPersonNotValidated" ), false );
42
+ testValidateArgs (target , List .of ("addIntValue" , "addPersonAndIntValue" , "addPersons" , "addNames " ), true );
43
+ testValidateArgs (target , List .of ("addPerson" , "getPerson" , "getIntValue" , "addPersonNotValidated" ), false );
43
44
}
44
45
45
46
@ Test
46
47
void shouldValidateArgsWithConstraintsOnInterface () {
47
48
Object target = new MyInterfaceImpl ();
48
- testShouldValidateArguments (target , List .of ("addIntValue" , "addPersonAndIntValue" , "addPersons" ), true );
49
- testShouldValidateArguments (target , List .of ("addPerson" , "addPersonNotValidated" , "getPerson" , "getIntValue" ), false );
49
+ testValidateArgs (target , List .of ("addIntValue" , "addPersonAndIntValue" , "addPersons" ), true );
50
+ testValidateArgs (target , List .of ("addPerson" , "addPersonNotValidated" , "getPerson" , "getIntValue" ), false );
50
51
}
51
52
52
53
@ Test
53
54
void shouldValidateReturnValueWithConstraintsDirectlyOnClass () {
54
55
Object target = new MyClass ();
55
- testShouldValidateReturnValue (target , List .of ("getPerson" , "getIntValue" ), true );
56
- testShouldValidateReturnValue (target , List .of ("addPerson" , "addIntValue" , "addPersonNotValidated" ), false );
56
+ testValidateReturnValue (target , List .of ("getPerson" , "getIntValue" ), true );
57
+ testValidateReturnValue (target , List .of ("addPerson" , "addIntValue" , "addPersonNotValidated" ), false );
57
58
}
58
59
59
60
@ Test
60
61
void shouldValidateReturnValueWithConstraintsOnInterface () {
61
62
Object target = new MyInterfaceImpl ();
62
- testShouldValidateReturnValue (target , List .of ("getPerson" , "getIntValue" ), true );
63
- testShouldValidateReturnValue (target , List .of ("addPerson" , "addIntValue" , "addPersonNotValidated" ), false );
63
+ testValidateReturnValue (target , List .of ("getPerson" , "getIntValue" ), true );
64
+ testValidateReturnValue (target , List .of ("addPerson" , "addIntValue" , "addPersonNotValidated" ), false );
64
65
}
65
66
66
67
@ Test
67
68
void classLevelValidatedAnnotation () {
68
69
Object target = new MyValidatedClass ();
69
- testShouldValidateArguments (target , List .of ("addPerson" ), false );
70
- testShouldValidateReturnValue (target , List .of ("getPerson" ), false );
70
+ testValidateArgs (target , List .of ("addPerson" ), false );
71
+ testValidateReturnValue (target , List .of ("getPerson" ), false );
71
72
}
72
73
73
- private static void testShouldValidateArguments (Object target , List <String > methodNames , boolean expected ) {
74
+ private static void testValidateArgs (Object target , List <String > methodNames , boolean expected ) {
74
75
for (String methodName : methodNames ) {
75
76
assertThat (getHandlerMethod (target , methodName ).shouldValidateArguments ()).isEqualTo (expected );
76
77
}
77
78
}
78
79
79
- private static void testShouldValidateReturnValue (Object target , List <String > methodNames , boolean expected ) {
80
+ private static void testValidateReturnValue (Object target , List <String > methodNames , boolean expected ) {
80
81
for (String methodName : methodNames ) {
81
82
assertThat (getHandlerMethod (target , methodName ).shouldValidateReturnValue ()).isEqualTo (expected );
82
83
}
@@ -113,6 +114,9 @@ public void addPersonAndIntValue(@Valid Person person, @Max(10) int value) {
113
114
public void addPersons (@ Valid List <Person > persons ) {
114
115
}
115
116
117
+ public void addNames (List <@ NotEmpty String > names ) {
118
+ }
119
+
116
120
public void addPersonNotValidated (Person person ) {
117
121
}
118
122
0 commit comments