org.springframework.mock.web

## Class MockFilterChain

- java.lang.Object
- - org.springframework.mock.web.MockFilterChain

- All Implemented Interfaces:
    FilterChain

      public class MockFilterChain
      extends Object
      implements FilterChain

  Mock implementation of the `FilterChain` interface. Used for testing the web framework; also useful for testing custom `Filter` implementations.

  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:
    `MockFilterConfig`, `PassThroughFilterChain`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `MockFilterChain()` Register a single do-nothing `Filter` implementation.
    `MockFilterChain(Servlet servlet)` Create a FilterChain with a Servlet.
    `MockFilterChain(Servlet servlet, Filter... filters)` Create a `FilterChain` with Filter's and a Servlet.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `doFilter(ServletRequest request, ServletResponse response)` Invoke registered `Filter`s and/or `Servlet` also saving the request and response.
    `ServletRequest` | `getRequest()` Return the request that `doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse)` has been called with.
    `ServletResponse` | `getResponse()` Return the response that `doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse)` has been called with.
    `void` | `reset()` Reset the `MockFilterChain` allowing it to be invoked again.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### MockFilterChain

          public MockFilterChain()

      Register a single do-nothing `Filter` implementation. The first invocation saves the request and response. Subsequent invocations raise an `IllegalStateException` unless `reset()` is called.

    - #### MockFilterChain

          public MockFilterChain(Servlet servlet)

      Create a FilterChain with a Servlet.
      Parameters:
        `servlet` - the Servlet to invoke
      Since:
        3.2

    - #### MockFilterChain

          public MockFilterChain(Servlet servlet,
                         Filter... filters)

      Create a `FilterChain` with Filter's and a Servlet.
      Parameters:
        `servlet` - the `Servlet` to invoke in this `FilterChain`
        `filters` - the `Filter`'s to invoke in this `FilterChain`
      Since:
        3.2

  - ### Method Detail

    - #### getRequest

          public ServletRequest getRequest()

      Return the request that `doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse)` has been called with.

    - #### getResponse

          public ServletResponse getResponse()

      Return the response that `doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse)` has been called with.

    - #### doFilter

          public void doFilter(ServletRequest request,
                      ServletResponse response)
                        throws IOException,
                               ServletException

      Invoke registered `Filter`s and/or `Servlet` also saving the request and response.
      Specified by:
        `doFilter` in interface `FilterChain`
      Throws:
        `IOException`
        `ServletException`

    - #### reset

          public void reset()

      Reset the `MockFilterChain` allowing it to be invoked again.