### Question:
MqFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { chain.doFilter(request, response); } catch (Exception e) { statService.start(); throw e; } }  @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void destroy();  }### Answer:
@Test public void doFilterTest() throws IOException, ServletException { StatService statService=mock(StatService.class); MqFilter mqFilter=new MqFilter(); ReflectionTestUtils.setField(mqFilter, "statService", statService); ServletRequest servletRequest=mock(ServletRequest.class); ServletResponse servletResponse=mock(ServletResponse.class); FilterChain filterChain=mock(FilterChain.class); mqFilter.doFilter(servletRequest, servletResponse, filterChain); verify(filterChain).doFilter(servletRequest, servletResponse); }
@Test public void doFilterErrorTest() throws IOException, ServletException { StatService statService=mock(StatService.class); MqFilter mqFilter=new MqFilter(); ReflectionTestUtils.setField(mqFilter, "statService", statService); ServletRequest servletRequest=mock(ServletRequest.class); ServletResponse servletResponse=mock(ServletResponse.class); FilterChain filterChain=mock(FilterChain.class); doThrow(new RuntimeException()).when(filterChain).doFilter(servletRequest, servletResponse); boolean rs=false; try { mqFilter.doFilter(servletRequest, servletResponse, filterChain); } catch (Exception e) { rs=true; } verify(statService).start(); assertEquals(true, rs); }