Testing Spring MVC @ExceptionHandler method with Spring MVC Test -


i have following simple controller catch unexpected exceptions:

@controlleradvice public class exceptioncontroller {      @exceptionhandler(throwable.class)     @responsestatus(value = httpstatus.internal_server_error)     @responsebody     public responseentity handleexception(throwable ex) {         return responseentityfactory.internalservererrorresponse("unexpected error has occurred.", ex);     } } 

i'm trying write integration test using spring mvc test framework. have far:

@runwith(mockitojunitrunner.class) public class exceptioncontrollertest {     private mockmvc mockmvc;      @mock     private statuscontroller statuscontroller;      @before     public void setup() {         this.mockmvc = mockmvcbuilders.standalonesetup(new exceptioncontroller(), statuscontroller).build();     }      @test     public void checkunexpectedexceptionsarecaughtandstatuscode500isreturnedinresponse() throws exception {          when(statuscontroller.checkhealth()).thenthrow(new runtimeexception("unexpected exception"));          mockmvc.perform(get("/api/status"))                 .anddo(print())                 .andexpect(status().isinternalservererror())                 .andexpect(jsonpath("$.error").value("unexpected exception"));     } } 

i register exceptioncontroller , mock statuscontroller in spring mvc infrastructure. in test method setup expectation throw exception statuscontroller.

the exception being thrown, exceptioncontroller isn't dealing it.

i want able test exceptioncontroller gets exceptions , returns appropriate response.

any thoughts on why doesn't work , how should kind of test?

thanks.

i had same issue , following works me:

@before public void setup() {     this.mockmvc = mockmvcbuilders.standalonesetup(statuscontroller)          .setcontrolleradvice(new exceptioncontroller())         .build(); } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

java - Using an Integer ArrayList in Android -