Let’s say you have a request class defined like this:
public class CreateUserRequest {
@NotNull
@Pattern("^[a-zA-Z0-9]+$")
@Size(min = 6, max = 16)
public String userName;
@NotNull
@Pattern("^[a-zA-Z0-9]+$")
@Size(min = 8, max = 32)
public String password;
@NotNull
@Pattern("^[a-zA-Z]+ [a-zA-Z]+$")
@Size(min = 3, max = 100)
public String fullName;
}
How do you write tests for constraints?
Do you write these tests at all?
Minor update
It’s not about constraints I’m using in this example. It’s about testing/not testing whatever constraints you have.
Hibernate validator allows validating individual fields, so for this case one of the possible solutions is to write tests for
userName,passwordandfullName. This approach is pretty silly though, you may check this for better approach: Using same constraints in multiple classes.