0

I would like to know how can I test to see if my controller is throwing an exception?

Of course, the exception will cause the controller to redirect to another page or will return 500 status code. In my case, it returns 500 Internal Server Error based on what FF says.

I tried saying that I expect this status code but it doesn't work. How can I achieve this?

mockMvc.perform(get("/p/get"))
    .andDo(print())
    .andExpect(status().is(500));

Edit:

Controller:

@RequestMapping("/get")
public List<A> get(){
  return myService.getP(0);
}

MyService - getP():

public List<A> getP(int l){
  if(l<=0){
    throw new IllegalArgumentException();
  }
}

Somehow I want to treat this exception.

tzortzik
  • 4,993
  • 9
  • 57
  • 88
  • 1
    How does it not work? What does it return instead? Could you post more code? – Xorty Apr 04 '15 at 17:18
  • What does your test-config look like? Do you bootstrap the production web-mvc config in your integration test? – fateddy Apr 04 '15 at 17:40
  • @Xorty I added a few pieces of code – tzortzik Apr 05 '15 at 09:28
  • Show us more code of the controller, are there other request mappings on class level? – Zarathustra Apr 05 '15 at 09:35
  • @Zarathustra Yes, they are on class level. I think there is a misunderstanding here because I need to throw that Exception and treat it somehow. But I need to take care of it in the tests also. – tzortzik Apr 05 '15 at 12:03
  • @tzortzik You should tell us what happens instead of what you expect. Also add the console output since you are calling `print()` this should give us an hint – Zarathustra Apr 05 '15 at 15:44

0 Answers0