I'm trying to write a unit test for a controllerAdvice, all the example I saw in the net are for an integration test, that's mean they are calling their main Rest controller and I dont want to do in that way. here is the test i'm trying to write :
public class ExceptionTest {
private MockHttpServletRequest servletRequest;
private MockHttpServletResponse servletResponse;
@Before
public void setup() {
this.servletRequest = new MockHttpServletRequest("GET", "/");
this.servletResponse = new MockHttpServletResponse();
}
@Test
public void controllerAdviceExceptionHandlerExceptionResolverTest () throws UnsupportedEncodingException {
StaticWebApplicationContext ctx = new StaticWebApplicationContext();
ctx.registerSingleton("exceptionHandler", MyControllerAdvice.class);
ctx.refresh();
ExceptionHandlerExceptionResolver resolver = createExceptionResolver();
resolver.setApplicationContext(ctx);
ServletRequestBindingException ex = new ServletRequestBindingException("message");
Assert.assertNotNull(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex));
}
private ExceptionHandlerExceptionResolver createExceptionResolver() {
ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver() {
protected ServletInvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handlerMethod, Exception exception) {
Method method = new ExceptionHandlerMethodResolver(MyControllerAdvice.class).resolveMethod(exception);
return new ServletInvocableHandlerMethod(new MyControllerAdvice(), method);
}
};
exceptionResolver.afterPropertiesSet();
return exceptionResolver;
}
My issue is that the resolver.resolveException(this.servletRequest, this.servletResponse, null, ex) is returning null however it should not! any idea ?