I am trying to write unit test for a validator class that I have. So within my UniqueEmailValidator class, I injected a @Service component to check if it exist.
The actual problem is deep down in the stack-trace that you've provided:
Caused by: java.lang.NoSuchMethodException: com.x.x.validator.UniqueEmailValidator.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
... 52 more
This error message is thrown because there is some code that is trying to instantiate the UniqueEmailValidator
class constructor without any parameters. The problem will be resolved by adding a default constructor to this class:
public class UniqueEmailValidator implements ConstraintValidator<UniqueEmail, String> {
public UniqueEmailValidator() {
}
...
}
Hope this helps!
You need to have a default non argument public constructor. If you need to pass a Spring component into your validator, you may face a problem in test.
I solved it by having a component which holds a service and has a static method to give the service.