Skip to content

Not able to unit test validations using MockMVC [SPR-13273] #17864

Closed
@spring-projects-issues

Description

@spring-projects-issues

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions