When using annotation based validation for a form bean, what is the best practice for unit-testing those beans in order to ensure that correct validations annotations are specified for each field?
For example, if you have:
public class MyForm {
@NotNull
private String name;
}
What is the best way to verify that @NotNull
is applied to it?
One obvious way is to create a validator, throw a null at it and expect it to fail. But in my view this is not the best way as you'll be testing the behaviour and implementation of @NotNull
using that rather than trusting the framework.
Ideally I would want to use reflection or a utility that gives me the possibility of just asserting that an @NotNull
(and any other) validation is applied to a given field, rather than having to send various combination of values that fail validation.
Is there an elegant way of doing this, or am I on the right track in general?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…