Skip to content
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

Closed
spring-projects-issues opened this issue Jul 24, 2015 · 8 comments
Closed
Assignees
Labels
in: test Issues in the test module status: duplicate A duplicate of another issue type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jul 24, 2015

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 :

  1. I have a mvc controller annotated with @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 in resourceId parameter annotated with @PathParam. Also this parameter is annotated with two other annotations for validating the path parameter.
    1. @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.
    2. it has @NotBlank validation which is org.hibernate validation to check if its not empty.

I am building standalone MockMvc setup and using the perform 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

@spring-projects-issues
Copy link
Collaborator Author

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?

@spring-projects-issues
Copy link
Collaborator Author

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 @NotBlank and @Pattern.

Hope I am clear now.If you need more information please let me know.

@spring-projects-issues
Copy link
Collaborator Author

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?

@spring-projects-issues
Copy link
Collaborator Author

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.

@spring-projects-issues
Copy link
Collaborator Author

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.
I understand that for primary key parameters/path parameters like resourceId I mentioned here Spring takes care of basic conversions. So resourceId should not be a String in first place. If its Long or Integer Spring will either convert or keep the value Null. And there I have to simply check null.
However we have converted this project from old Jersey Project which had this implementation and for strange /weird reasons our architect doesn't want to change this and wants us to use Strings and apply @Pattern validations on these Parameters. Hence the conversion is happening inside the method. So much about why conversion is happening inside the method. Anyway thats not the point.
What I want is to be able to validate all types of validations (like @NotBlank @NotNull,@Size(min = 1, max = 40),@Pattern) applied on Path Parameters to execute and it should generate appropriate error response which I can validate in my unit test. I guess JSR 330 says it should be 400 Bad Request response if any such validations fail. I am really not concerned about following any standard as such as long as I am able to test against consistent error response when validations fail.

I hope I am able to get my points across. Please let me know if they are invalid outright or make sense. Please let me know if you need more information as well. I am anyway creating a repro project for this.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jul 28, 2015

Rossen Stoyanchev commented

Ah, I think you're trying to apply annotation-based validation on @PathParameter in which case this is a duplicate of #11041 (not resolved) which in turn depends on #16519.

@spring-projects-issues
Copy link
Collaborator Author

Shailesh Vaishampayan commented

So if these features are implemented MockMVC also will start giving me validations failure results correct?

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Yes.

@spring-projects-issues spring-projects-issues added in: test Issues in the test module type: enhancement A general enhancement status: duplicate A duplicate of another issue labels Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module status: duplicate A duplicate of another issue type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants