@@ -15,30 +15,31 @@ An [AssertJ](https://joel-costigliola.github.io/assertj/) extension for [Bean-Va
1515## Coordinates
1616
1717``` xml
18+
1819<groupId >com.github.jinahya</groupId >
1920<artifactId >assertj-bean-validation</artifactId >
2021```
2122
2223### Classifiers
2324
24- classifier | ` - release` | ` javax.* ` | ` jakarta.* ` |notes
25- ---------------------|------------ |-----------|-------------|-----
26- NONE | 8 | ✓ | |
27- ` jakarta ` | 8 | | ✓ |
28- ` release-11 ` | 11 | ✓ | |
29- ` release-11-jakarta ` | 11 | | ✓ |
30- ` release-17 ` | 17 | ✓ | |
31- ` release-17-jakarta ` | 17 | | ✓ |
25+ classifier | ` release ` | ` javax.* ` | ` jakarta.* ` |notes
26+ ---------------------|-----------|-----------|-------------|-----
27+ NONE | 8 | ✓ | |
28+ ` jakarta ` | 8 | | ✓ |
29+ ` release-11 ` | 11 | ✓ | |
30+ ` release-11-jakarta ` | 11 | | ✓ |
31+ ` release-17 ` | 17 | ✓ | |
32+ ` release-17-jakarta ` | 17 | | ✓ |
3233
3334## Compatibilities
3435
35- * Depends (as ` provided ` ) on
36+ * Depends, in ` provided ` scope, on
3637 the [ latest org.assertj: assertj-core ] ( https://javadoc.io/doc/org.assertj/assertj-core/latest/index.html ) .
3738* Targets ** Java 8** .
3839
3940### JDK
4041
41- This module requires the ** latest JDK** (currently, 18 ), especially with its test sources, for building itself.
42+ This module requires the ** latest JDK** (currently, 19 ), especially with its test sources, for building itself.
4243
4344## Usages
4445
@@ -50,16 +51,12 @@ class User {
5051 @Max (0x7F ) @PositiveOrZero int age;
5152}
5253
53- class Registration {
54+ abstract class Registration {
5455 @Valid @NotNull User user;
5556}
5657
57- class SeniorRegistration
58- extends Registration {
59- @AssertTrue
60- boolean isUserSenior () {
61- return user == null || user. age >= 60 ;
62- }
58+ class SeniorRegistration extends Registration {
59+ @Senior User getUser () { return super . getUser(); }
6360}
6461```
6562
@@ -69,160 +66,85 @@ Verifies that actual bean objects and/or their properties are valid.
6966
7067``` java
7168class Test {
72-
73- @Test
74- void test () {
69+ @Test void test1 () {
7570 assertThatBean(new User (" Jane" , 28 )) // valid
76- .isValid() // passes
77- .hasValidProperty(" name" ) // passes
78- .hasValidProperty(" age" ); // passes
79- }
80- }
81- ```
82-
83- You can debug by analyzing (or verifying) constraint violations populated while validating.
84-
85- ``` java
86- class Test {
87-
88- @Test
89- void test () {
90- assertThatBean(new User (" " , 27 )) // invalid; name is blank
91- .isValid(cv - > { // should fail
92- assertThat(cv. getInvalidValue()). isEqualTo(actual. getName());
93- assertThat(cv. getPropertyPath())
94- .isNotEmpty()
95- .allSatisfy(n - > {
96- assertThat(n. getName()). isEqualTo(" name" );
97- });
98- });
99- assertThatBean(new User (" John" , 300 )) // invalid; too old to be true
100- .hasValidProperty(" age" , cv - > { // should fail
101- assertThat(cv. getInvalidValue()). isEqualTo(actual. getAge());
102- assertThat(cv. getPropertyPath())
103- .isNotEmpty()
104- .allSatisfy(n - > assertThat(n. getName()). isEqualTo(" age" ));
105- });
71+ .isValid() // should pass
72+ .hasValidProperty(" name" ) // should pass
73+ .hasValidProperty(" age" ); // should pass
10674 }
10775}
10876```
10977
110- ### Using extended assertion classes
111-
112- You can work with your own (extended) assertion class.
113-
114- ``` java
115- class UserAssert
116- extends AbstractBeanAssert<UserAssert , User > {
117-
118- UserAssert (User actual ) {
119- super (actua, UserAssert . class);
120- }
121-
122- UserAssert isNamedJane () {
123- return isNotNull()
124- .is(new Condition<> (v - > " Jane" . equalsIgnoreCase(v. getName()),
125- " named Jane" ));
126- }
127- }
128- ```
129-
130- A number of static factory methods are prepared for extended assertion classes.
131-
132- ``` java
133- class Test {
134-
135- @Test
136- void test () {
137- // specify your assert class along with specific actual class
138- {
139- final User actual = new User (" Jane" , 0 );
140- assertThatBean(UserAssert . class, User . class, actual)
141- .isValid()
142- .hasValidProperty(" name" )
143- .hasValidProperty(" age" )
144- .isNamedJane(); // should pass
145- }
146- // or emit the actual class
147- {
148- final User actual = new User (" John" , 1 );
149- assertThatBean(UserAssert . class, actual)
150- .isValid()
151- .hasValidProperty(" name" )
152- .hasValidProperty(" age" )
153- .isNamedJane(); // should fail
154- }
155- // or let it find whatever required
156- {
157- // tries to find a class named `UserAssert`
158- // in the same package of `User` class
159- final Object actual = new User (" Jane" , 0 ); // java.lang.Object
160- assertThatVirtualBean(actual) // mind the name of the method
161- .isValid()
162- .hasValidProperty(" name" )
163- .hasValidProperty(" age" )
164- .isNamedJane();
165- }
166- }
167- }
168- ```
169-
170- ### Verifying values against properties
78+ ### Verifying a value against specific property
17179
17280You can verify a value against specific property of specific bean type.
17381
17482``` java
17583class Test {
176-
177- @Test
178- void test () {
84+ @Test void test2 () {
17985 assertThatProperty(" John" ). isValidFor(User . class, " name" ); // should pass
180- assertThatProperty(null ). isValidFor(User . class, " name" ); // should fail; @NotBlank
181- assertThatProperty(" " ). isValidFor(User . class, " name" ); // should fail; @NotBlank
182- assertThatProperty(31 ). isValidFor(User . class, " age" ); // should pass
183- assertThatProperty(- 1 ). isValidFor(User . class, " age" ); // should fail; @Min(0x00)
184- assertThatProperty(297 ). isValidFor(User . class, " age" ); // should fail; @Max(0x80)
86+ assertThatProperty( null ). isValidFor(User . class, " name" ); // should fail; @NotBlank
87+ assertThatProperty( " " ). isValidFor(User . class, " name" ); // should fail; @NotBlank
88+ assertThatProperty( " " ). isValidFor(User . class, " name" ); // should fail; @NotBlank
89+ assertThatProperty( 31 ). isValidFor(User . class, " age" ); // should pass
90+ assertThatProperty( - 1 ). isValidFor(User . class, " age" ); // should fail; @PositiveOrZero
91+ assertThatProperty( 297 ). isValidFor(User . class, " age" ); // should fail; @Max(0x7F)
18592 }
18693}
18794```
18895
189- Note that a bean value can also be validated against a property of another bean .
96+ Note that a bean value can also be validated against a property of another type .
19097
19198``` java
19299class Test {
193-
194- @Test
195- void test () {
196- assertThatBean(null ). isValidFor(Registration . class, " user" ); // should fail; @NotNull
100+ @Test void test3 () {
101+ assertThatBean(new User (" Jane" , 21 ))
102+ .isValidFor(Registration . class, " user" ); // should pass
103+ assertThatBean(null ). isValidFor(Registration . class, " user" ); // should fail; @NotNull
104+ assertThatBean(new User (" John" , 59 ))
105+ .isValidFor(SeniorRegistration . class, " user" ); // should fail; age should be >= 60
197106 }
198107}
199108```
200109
201- Note also that the ` @Valid ` is not honored by ` Validator#validateProperty ` method nor ` Validator#validateValue ` method.
110+ Note also that the ` @Valid ` annotation is not honored by ` Validator#validateProperty ` method nor ` Validator#validateValue ` method.
202111See [ 6.1.1. Validation methods] (Jakarta Bean Validation specification).
203112
204113``` java
205114class Test {
206-
207- @Test
208- void test () {
115+ @Test void test4 () {
209116 User user = new User (" John" , 300 ); // not valid, obviously
210117 assertThatBean(user). isValid(); // so does fail
211118 assertThatBean(user). isValidFor(Registration . class, " user" ); // DOES NOT FAIL!
212119 assertThatBean(null ). isValidFor(Registration . class, " user" ); // while fails
213- assertThatBean(new Registration (user)). isValid(); // fails, after ...
120+ assertThatBean(new SeniorRegistration (user)). isValid(); // should fail
214121 }
215122}
216123```
217124
218- ### Using a ` Validator ` and/or targeting groups
125+ ### Using a specific ` Validator `
126+
127+ ``` java
128+ assertThatBean(user)
129+ .usingValidator(... ) // null to reset
130+ .isValid();
131+ ```
132+
133+ ### Using validation groups
134+
135+ ``` java
136+ assertThatBean(user)
137+ .targetingGroups(... , ... ) // null or empty to reset
138+ .isValid();
139+ ```
219140
220- You can configure a ` Validator ` or targeting groups for validating.
141+ ### Consuming constraint violations
221142
222143``` java
223144assertThatBean(user)
224- .usingValidator(... ) // null to reset
225- .targetingGroups(... ,... ) // null or empty to reset
145+ .consumingViolations(cv - > {
146+ // ... debug
147+ })
226148 .isValid();
227149```
228150
0 commit comments