Class MockFilterChain

java.lang.Object
org.springframework.mock.web.MockFilterChain
All Implemented Interfaces:
jakarta.servlet.FilterChain

public class MockFilterChain extends Object implements jakarta.servlet.FilterChain
Mock implementation of the FilterChain interface.

A MockFilterChain can be configured with one or more filters and a Servlet to invoke. The first time the chain is called, it invokes all filters and the Servlet, and saves the request and response. Subsequent invocations raise an IllegalStateException unless reset() is called.

Since:
2.0.3
Author:
Juergen Hoeller, Rob Winch, Rossen Stoyanchev
See Also:
  • Constructor Details

    • MockFilterChain

      public MockFilterChain()
      Create an empty MockFilterChain without any filters.
    • MockFilterChain

      public MockFilterChain(jakarta.servlet.Servlet servlet)
      Create a MockFilterChain with a Servlet.
      Parameters:
      servlet - the Servlet to invoke
      Since:
      3.2
    • MockFilterChain

      public MockFilterChain(jakarta.servlet.Servlet servlet, jakarta.servlet.Filter... filters)
      Create a MockFilterChain with a Servlet and filters.
      Parameters:
      servlet - the Servlet to invoke in this MockFilterChain
      filters - the filters to invoke in this MockFilterChain
      Since:
      3.2
  • Method Details

    • getRequest

      public @Nullable jakarta.servlet.ServletRequest getRequest()
      Return the request that doFilter(ServletRequest, ServletResponse) has been called with.
    • getResponse

      public @Nullable jakarta.servlet.ServletResponse getResponse()
      Return the response that doFilter(ServletRequest, ServletResponse) has been called with.
    • doFilter

      public void doFilter(jakarta.servlet.ServletRequest request, jakarta.servlet.ServletResponse response) throws IOException, jakarta.servlet.ServletException
      Invoke registered Filters and/or Servlet also saving the request and response.
      Specified by:
      doFilter in interface jakarta.servlet.FilterChain
      Throws:
      IOException
      jakarta.servlet.ServletException
    • reset

      public void reset()
      Reset this MockFilterChain allowing it to be invoked again.