You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+243Lines changed: 243 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,13 +7,256 @@ Testing library for Spring Boot projects.
7
7
Add this library to the classpath by adding the following maven dependency. Versions can be found [here](../../packages)
8
8
9
9
```xml
10
+
10
11
<dependency>
11
12
<groupId>it.aboutbits</groupId>
12
13
<artifactId>spring-boot-testing</artifactId>
13
14
<version>x.x.x</version>
14
15
</dependency>
15
16
```
16
17
18
+
## Usage
19
+
20
+
### Validation
21
+
22
+
The validation tester allows us to quickly test simple validation constraints. Most commonly we use bean validation for this.
23
+
24
+
#### Configuration
25
+
26
+
To use the validation tester in your project you need to extend both the [BaseValidationAssert.java](src/main/java/it/aboutbits/springboot/testing/validation/core/BaseValidationAssert.java) and the [BaseRuleBuilder.java](src/main/java/it/aboutbits/springboot/testing/validation/core/BaseRuleBuilder.java).
By default, the validation tester will assume that all properties of type `Record` are substructures. Therefore, using the `@Valid` annotation is required to make sure that validation for those records is triggered.
45
+
You can add a class to a whitelist to disable this behavior:
Each property is required to have at least one rule defined. You can add multiple rules for the same property as needed to combine more complex rulesets.
67
+
The tester will fail if not all properties have rules. In case you have properties without any restrictions, use the `notValidated` rule.
68
+
69
+
The validation tester works by taking in a **valid** parameter. It will then mutate the parameter internally and test each property with an invalid value. Then a check is done if a validation violation is raised as expected.
70
+
71
+
In any case, the call to `isCompliant` is required at the end and then triggers the actual assertion.
72
+
73
+
You can use plain bean validation to verify a Record:
74
+
75
+
```java
76
+
importjakarta.validation.constraints.Future;
77
+
importjakarta.validation.constraints.NotNull;
78
+
importjakarta.validation.constraints.Past;
79
+
importorg.springframework.lang.Nullable;
80
+
81
+
public record SomeParameter(
82
+
@NotBlank
83
+
String name,
84
+
@Min(18)
85
+
int age,
86
+
@NotNull
87
+
@Past
88
+
LocalDate birthDay,
89
+
@Nullable
90
+
String something,
91
+
String notValidatedAtAll
92
+
) {
93
+
}
94
+
95
+
96
+
@Test
97
+
void testValidation() {
98
+
var validParameter =newSomeParameter("Sepp", 32);
99
+
100
+
assertThatValidation().of(validParameter)
101
+
.usingBeanValidation()
102
+
.notBlank("name")
103
+
.min("age", 18)
104
+
.notNull("birthDay")
105
+
.past("birthDay")
106
+
.nullable("something")
107
+
.notValidated("notValidatedAtAll")
108
+
.isCompliant();
109
+
}
110
+
```
111
+
112
+
Alternatively you can use a method call to a service function to verify the validation. This is the preferred way as it makes sure that the bean validation is both triggered and also valid.
The `Rule` requires the property name, a value-source and an array of optional parameters. For example `min(property, minValue)` takes in the additional parameter for the value.
178
+
Note that the value-source must return **invalid** values. This is required because the tool is actively trying to violate the rules to check if an error is raised.
179
+
180
+
#### Adding custom value sources
181
+
182
+
You can add custom values sources by implementing the `ValueSource` interface.
183
+
While the interface can not enforce the static function `registerType`, it is best practice to implement it in a way that keeps this extensible.
184
+
This way we can use the same logical value-source for multiple property types.
var sourceFunction =TYPE_SOURCES.get(propertyClass);
215
+
if (sourceFunction !=null) {
216
+
return (Stream<T>) sourceFunction.apply(args);
217
+
}
218
+
219
+
thrownewIllegalArgumentException("Property class not supported!");
220
+
}
221
+
}
222
+
```
223
+
224
+
#### Adding support for custom types
225
+
226
+
_Note: CustomType wrappers from the `toolbox` are currently not natively supported._
227
+
228
+
Adding custom types will require some extension to the existing value-sources. Those need to become aware of the new type in order to produce values of said type.
0 commit comments