4

I have the following controller:

class Controller {

    @ResponseStatus(HttpStatus.OK)
    @RequestMapping(value = "/verifyCert", method = RequestMethod.GET)
    public void verifyCertificate() throws CertificateExpiredException, CertificateNotYetValidException {
        certificate.checkValidity();
    }

    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ExceptionHandler({CertificateExpiredException.class, CertificateNotYetValidException.class})
    public void handleCertificateValidityException(Exception e) {}
}

My goal is to that controller redirects to exception handler if certificate is not valid.

fashuser
  • 2,152
  • 3
  • 29
  • 51

3 Answers3

3

When using standaloneSetup all you are doing is performing the setup for the specific controller.

If you don't want to configure the whole application context (which you would do with webAppContextSetup instead of standaloneSetup), you can manually setup the exception handler by changing your code to:

 @Before
 public void setup() throws IOException {
     MockitoAnnotations.initMocks(this);
     mockMvc = MockMvcBuilders.standaloneSetup(controller).setHandlerExceptionResolvers(new ExceptionHandlerExceptionResolver()).build();
 }

 @Test
 public void test() throws Exception {
     mockMvc.perform(get("/verifyCert.controller").contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)).andExpect(status().isForbidden());
 }

This works because ExceptionHandlerExceptionResolver is the class that Spring MVC uses to handle exceptions based on the @ExceptionHandler annotation

Check out one of my older relevant answers that covers an ever more difficult case (the use of @ControllerAdvice on a class that contains @ExceptionHandler).

Community
  • 1
  • 1
geoand
  • 60,071
  • 24
  • 172
  • 190
  • Thanks, I've tried your approach. But unfortunatly I get the following error: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException – fashuser Sep 05 '14 at 15:11
  • @fashuser That's weird! I ran this code and everything worked as expected! Can you update your question with this info? – geoand Sep 05 '14 at 15:12
  • I've added full stacktrace to question. – fashuser Sep 05 '14 at 15:22
  • @fashuser What version of Spring are you using? – geoand Sep 05 '14 at 16:36
  • @fashuser I was not able to reproduce your problem with either Spring 4 or Spring 3.2 – geoand Sep 05 '14 at 17:07
-1

A property in your class net.scansafe.scancentre21.springmvc.web.easyid.ManageSamlAuthenticationRealmController.verifySecondarySamlSpCertificate(ManageSamlAuthenticationRealmController.java) is null.

You can debug the method of the class to find it. You need to add a @Autowired to this property for injection.

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
Dahai
  • 47
  • 2
-1

I've Solved In this way :

@Test(expected=MyException.class)
public void shouldThrowException() throws IOException, Exception {

    //Mock Response Controller 
    when(client.mymethod(any(MyInputClassOfController.class)))
        .thenReturn(mockedResponse);

    when(innerClassInController.myMethodThatInvokeTheException(mock(MyClass.class)))
        .thenThrow(new MyException("message"));

    this.mockMvc.perform(post("/root/search/")
        .contentType(MediaType.APPLICATION_JSON_UTF8)
        .content( "null Request to throw the exception"))
        .andExpect(status().isBadRequest());
}
MCO
  • 1,187
  • 1
  • 11
  • 20
effedetto
  • 1
  • 2