-
Notifications
You must be signed in to change notification settings - Fork 38.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not able to unit test validations using MockMVC [SPR-13273] #17864
Comments
Rossen Stoyanchev commented The SO post indicates a NumberFormatException was raised. The above description says validations do not execute. Can you please update the above description with all the necessary information? |
Shailesh Vaishampayan commented Well.. sorry I didn't explain it in detail. In fact I also receive the NumberFormatException. In my deleteResource method I convert Path Parameter resourceId which is a String, into Integer. Now if I send resourceId as something which cannot be converted to Integer (e.g. abcd) instead of regular integer it throws NumberFormatException. However it should not reach the point in my method where it throws NumberFormatException if validations are executed as it would preempt the call and should return response which indicates Validaiton errors. also in the link I have given they are using a bean and annotate it with same validation annotation that I am using like Hope I am clear now.If you need more information please let me know. |
Shailesh Vaishampayan commented In your MVC showcase GIthub repository you have given example to explain how to write unit tests to validate parameters . I have not tried that but I took a cue from it and assumed that It should work for PathParameters which are annotated with javax..and hibernate validations as well. But my assumption seems to fail and would like to know if its possible? |
Rossen Stoyanchev commented Okay so the SO link is not your post. I'm afraid I'm not any more clear than before. Please, provide the controller method signature and implementation if relevant (I'm not sure why conversion needs to be performed inside the controller method). Ideally provide a simple repro project. |
Shailesh Vaishampayan commented Ok I will create a repro project. As far as the conversion is concerned this will be a distraction from original requirement of unit testing validations (javax and hibernate ) applied to Path parameters. Still will provide info on why conversion is happening inside the method.
|
Rossen Stoyanchev commented Ah, I think you're trying to apply annotation-based validation on |
Shailesh Vaishampayan commented So if these features are implemented MockMVC also will start giving me validations failure results correct? |
Rossen Stoyanchev commented Yes. |
Shailesh Vaishampayan opened SPR-13273 and commented
I am starting out on using
MockMvc
, and I don't understand why my validations (javax and hibernate) do not execute when I run my unit test.Here is the summary :
@RestController
. This controller has a method to delete resource. uri to delete resource is/myresources/{resourceId}
. I have given context relative url. This method accepts the Path Parameter inresourceId
parameter annotated with@PathParam
. Also this parameter is annotated with two other annotations for validating the path parameter.@Pattern
which is a javax.validation annotation which specifies following pattern:regexp=\[0-9\]{1,12}
to accept only 1 to 12 digits as input.@NotBlank
validation which is org.hibernate validation to check if its not empty.I am building standalone
MockMvc
setup and using theperform
method to call the url with path parameter.Should it not preempt the call by carrying out validation so that it doesn't reach my method?
In fact I receive the NumberFormatException in my deleteResource method. I convert Path Parameter resourceId which is a String, into Integer in my deleteResource method..Now if I send resourceId as something which cannot be converted to Integer (e.g. abcd) instead of regular integer it throws NumberFormatException. However it should not reach inside my method where it throws NumberFormatException.(rather it happens at the first line itself where I convert String into Integer) if validations are executed as it would pre-empt the call and should return response which indicate validation errors.
If yes, why is it not working? If no, is this possible in anyway because I want to write a unit test for each of the validations?
I found similar issue on Stack Overflow.
Reference URL: http://stackoverflow.com/questions/27106298/mock-mvc-javax-validation-test
The text was updated successfully, but these errors were encountered: