SystemExceptionResolver extends SimpleMappingExceptionResolver { protected void setExceptionCode(Exception ex, HttpServletRequest request, HttpServletResponse response) { String exceptionCode = null; if (exceptionCodeResolver != null && (StringUtils.hasText( exceptionCodeAttribute) || StringUtils.hasText( exceptionCodeHeader))) { exceptionCode = exceptionCodeResolver.resolveExceptionCode(ex); } if (exceptionCode == null) { return; } if (StringUtils.hasText(exceptionCodeAttribute)) { request.setAttribute(exceptionCodeAttribute, exceptionCode); FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request); if (flashMap != null) { flashMap.put(exceptionCodeAttribute, exceptionCode); } } if (StringUtils.hasText(exceptionCodeHeader)) { response.setHeader(exceptionCodeHeader, exceptionCode); } }  void setExceptionCodeAttribute(String exceptionCodeAttribute); void setExceptionCodeHeader(String exceptionCodeHeader); void setResultMessagesAttribute(String resultMessagesAttribute); void setExceptionCodeResolver(
            ExceptionCodeResolver exceptionCodeResolver);  }
@Test public void testSetExceptionCode_exception_code_is_null() { FlashMap flashMap = new FlashMap(); mockRequest.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, flashMap); SystemException occurException = new SystemException(null, "message"); testTarget.setExceptionCode(occurException, mockRequest, mockResponse); Enumeration<String> attributeNames = mockRequest.getAttributeNames(); assertThat(attributeNames.nextElement().equals( DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE), is(true)); assertThat(attributeNames.hasMoreElements(), is(false)); assertThat(mockResponse.getHeaderNames().isEmpty(), is(true)); assertThat(flashMap.isEmpty(), is(true)); }
@Test public void testSetExceptionCode_exception_code_is_notnull() { FlashMap flashMap = new FlashMap(); mockRequest.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, flashMap); SystemException occurException = new SystemException("code001", "message"); testTarget.setExceptionCode(occurException, mockRequest, mockResponse); assertThat(mockRequest.getAttribute("exceptionCode").toString(), is( "code001")); assertThat(mockResponse.getHeader("X-Exception-Code"), is("code001")); assertThat((String) flashMap.get("exceptionCode"), is("code001")); }