I have some logic inside the interceptor's postHandle method to expose a header value to the my front end angular application. My question is how to test interceptors?
public class CustomResponseInterceptor implements HandlerInterceptor {
private final String ADMIN_APP = "ADMIN_APP";
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object,
ModelAndView modelAndView) throws Exception {
AdminApp adminApp = loadAdminApp();
if(adminApp != null && adminApp.isValidAdminApp()){
response.addHeader(ADMIN_APP, adminApp.getAppName());
response.addHeader("Access-Control-Expose-Headers", ADMIN_APP);
}
}
}
Below is my testcase to check the add name is in header or not
@Test
void checkAdminAppNameInHeaderTest() throws Exception {
HandlerInterceptor delegate = mock(HandlerInterceptor.class);
new MappedInterceptor(null, delegate).postHandle(
mock(HttpServletRequest.class), mock(HttpServletResponse.class), null, mock(ModelAndView.class));
String adminAppName = response.getHeader(ADMIN_APP);
assertNotNull(adminAppName );
}
However the header always returns null mostly because the request and response are mocked here. How can i traverse through interceptor logic so that the postHandle method is called?