TenantContextHandlerInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { TenantContext context = determineTenantContext(request); this.logger.debug("Using context: {}", context); TenantContextHolder.setContext(context); return true; } TenantContextHandlerInterceptor(List<TenantIdentificationStrategy> strategies); @Override boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler); @Override void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex); void setThrowExceptionOnMissingTenant(boolean throwExceptionOnMissingTenant); boolean isThrowExceptionOnMissingTenant();  }
@Test public void whenContextFoundThenTheContextShouldBeSet() throws Exception { when(this.strategy.getTenant(this.request)).thenReturn("test"); this.interceptor.preHandle(this.request, this.response, null); assertThat(TenantContextHolder.getContext().getTenant(), is("test")); }
@Test(expected = IllegalStateException.class) public void whenNoContextFoundThenAnExceptionShouldBeThrown() throws Exception { when(this.strategy.getTenant(this.request)).thenReturn(null); this.interceptor.preHandle(this.request, this.response, null); }