java - Spring Boot 1.4 Unit Test Hibernate Validation WebMvcTest -


i trying perform unit test in spring boot 1.4 test validation returns 400 on invalid query string parameter.

controller

@restcontroller @validated public class examplecontroller {      ...      @requestmapping(value = "/example", method = get)     public response getexample(             @requestparam(value = "userid", required = true) @valid @pattern(regexp = my_regex) string segmentsrequest {          // stuff here     }  } 

exception handler

@controlleradvice @component public class globalexceptionhandler {      private static final logger logger = loggerfactory.getlogger(globalexceptionhandler.class);      // 400 - bad request     @exceptionhandler(value = {constraintviolationexception.class})     @responsestatus(httpstatus.bad_request)     public void constrainviolationhandle(httpservletrequest request, constraintviolationexception exception) {         logger.error("error bad request (400)");     }  } 

context

@bean public validator validator() {     final validatorfactory validatorfactory = validation.bydefaultprovider()             .configure()             .parameternameprovider(new reflectionparameternameprovider())             .buildvalidatorfactory();     return validatorfactory.getvalidator(); }  @bean public methodvalidationpostprocessor methodvalidationpostprocessor() {     final methodvalidationpostprocessor methodvalidationpostprocessor = new methodvalidationpostprocessor();     methodvalidationpostprocessor.setvalidator(validator());     return methodvalidationpostprocessor; } 

unit test

@runwith(springrunner.class) @webmvctest(examplecontroller.class) public class examplecontrollertest {      private static final string empty = "";      @autowired     private mockmvc mvc;      @test     public void test() throws exception {          // perform request         resultactions response = this.mvc.perform(             get("/example").param("userid", "invalid")         );          // assert result         response.andexpect(status().isbadrequest())                 .andexpect(content().string(empty));     }  } 

however when run test 200 not 400. validation not performed when running application not test.

i believe may due not picking 2 validation beans when performing test? validation works

@webmvctest annotation using spring boot wrapper on top of called standalone mockmvc configuration. mockmvc feature tests standalone controller without other beans.

to able test wider web configuration need use web application setup:

@runwith(springrunner.class) @webappconfiguration @contextconfiguration("my-servlet-context.xml") public class mywebtests {      @autowired     private webapplicationcontext wac;      private mockmvc mockmvc;      @before     public void setup() {         this.mockmvc = mockmvcbuilders.webappcontextsetup(this.wac).build();     }      // ...  } 

but notice such web application setup doesn't pick beans. example need explicitly register servler filters or spring security. believe validation should included.


Comments