Class MockPageContext

java.lang.Object
jakarta.servlet.jsp.JspContext
jakarta.servlet.jsp.PageContext
org.springframework.mock.web.MockPageContext

public class MockPageContext extends jakarta.servlet.jsp.PageContext
Mock implementation of the PageContext interface. Only necessary for testing applications when testing custom JSP tags.

Note: Expects initialization via the constructor rather than via the PageContext.initialize method. Does not support writing to a JspWriter, request dispatching, or handlePageException calls.

Since:
1.0.2
Author:
Juergen Hoeller
  • Constructor Details

    • MockPageContext

      public MockPageContext()
    • MockPageContext

      public MockPageContext(@Nullable jakarta.servlet.ServletContext servletContext)
      Create new MockPageContext with a default MockHttpServletRequest, MockHttpServletResponse, MockServletConfig.
      Parameters:
      servletContext - the ServletContext that the JSP page runs in (only necessary when actually accessing the ServletContext)
    • MockPageContext

      public MockPageContext(@Nullable jakarta.servlet.ServletContext servletContext, @Nullable jakarta.servlet.http.HttpServletRequest request)
      Create new MockPageContext with a MockHttpServletResponse, MockServletConfig.
      Parameters:
      servletContext - the ServletContext that the JSP page runs in
      request - the current HttpServletRequest (only necessary when actually accessing the request)
    • MockPageContext

      public MockPageContext(@Nullable jakarta.servlet.ServletContext servletContext, @Nullable jakarta.servlet.http.HttpServletRequest request, @Nullable jakarta.servlet.http.HttpServletResponse response)
      Create new MockPageContext with a MockServletConfig.
      Parameters:
      servletContext - the ServletContext that the JSP page runs in
      request - the current HttpServletRequest
      response - the current HttpServletResponse (only necessary when actually writing to the response)
    • MockPageContext

      public MockPageContext(@Nullable jakarta.servlet.ServletContext servletContext, @Nullable jakarta.servlet.http.HttpServletRequest request, @Nullable jakarta.servlet.http.HttpServletResponse response, @Nullable jakarta.servlet.ServletConfig servletConfig)
      Create new MockServletConfig.
      Parameters:
      servletContext - the ServletContext that the JSP page runs in
      request - the current HttpServletRequest
      response - the current HttpServletResponse
      servletConfig - the ServletConfig (hardly ever accessed from within a tag)
  • Method Details

    • initialize

      public void initialize(jakarta.servlet.Servlet servlet, jakarta.servlet.ServletRequest request, jakarta.servlet.ServletResponse response, String errorPageURL, boolean needsSession, int bufferSize, boolean autoFlush)
      Specified by:
      initialize in class jakarta.servlet.jsp.PageContext
    • release

      public void release()
      Specified by:
      release in class jakarta.servlet.jsp.PageContext
    • setAttribute

      public void setAttribute(String name, @Nullable Object value)
      Specified by:
      setAttribute in class jakarta.servlet.jsp.JspContext
    • setAttribute

      public void setAttribute(String name, @Nullable Object value, int scope)
      Specified by:
      setAttribute in class jakarta.servlet.jsp.JspContext
    • getAttribute

      public @Nullable Object getAttribute(String name)
      Specified by:
      getAttribute in class jakarta.servlet.jsp.JspContext
    • getAttribute

      public @Nullable Object getAttribute(String name, int scope)
      Specified by:
      getAttribute in class jakarta.servlet.jsp.JspContext
    • findAttribute

      public @Nullable Object findAttribute(String name)
      Specified by:
      findAttribute in class jakarta.servlet.jsp.JspContext
    • removeAttribute

      public void removeAttribute(String name)
      Specified by:
      removeAttribute in class jakarta.servlet.jsp.JspContext
    • removeAttribute

      public void removeAttribute(String name, int scope)
      Specified by:
      removeAttribute in class jakarta.servlet.jsp.JspContext
    • getAttributesScope

      public int getAttributesScope(String name)
      Specified by:
      getAttributesScope in class jakarta.servlet.jsp.JspContext
    • getAttributeNames

      public Enumeration<String> getAttributeNames()
    • getAttributeNamesInScope

      public Enumeration<String> getAttributeNamesInScope(int scope)
      Specified by:
      getAttributeNamesInScope in class jakarta.servlet.jsp.JspContext
    • getOut

      public jakarta.servlet.jsp.JspWriter getOut()
      Specified by:
      getOut in class jakarta.servlet.jsp.JspContext
    • getELContext

      public @Nullable jakarta.el.ELContext getELContext()
      Specified by:
      getELContext in class jakarta.servlet.jsp.JspContext
    • getSession

      public jakarta.servlet.http.HttpSession getSession()
      Specified by:
      getSession in class jakarta.servlet.jsp.PageContext
    • getPage

      public Object getPage()
      Specified by:
      getPage in class jakarta.servlet.jsp.PageContext
    • getRequest

      public jakarta.servlet.ServletRequest getRequest()
      Specified by:
      getRequest in class jakarta.servlet.jsp.PageContext
    • getResponse

      public jakarta.servlet.ServletResponse getResponse()
      Specified by:
      getResponse in class jakarta.servlet.jsp.PageContext
    • getException

      public @Nullable Exception getException()
      Specified by:
      getException in class jakarta.servlet.jsp.PageContext
    • getServletConfig

      public jakarta.servlet.ServletConfig getServletConfig()
      Specified by:
      getServletConfig in class jakarta.servlet.jsp.PageContext
    • getServletContext

      public jakarta.servlet.ServletContext getServletContext()
      Specified by:
      getServletContext in class jakarta.servlet.jsp.PageContext
    • forward

      public void forward(String path) throws jakarta.servlet.ServletException, IOException
      Specified by:
      forward in class jakarta.servlet.jsp.PageContext
      Throws:
      jakarta.servlet.ServletException
      IOException
    • include

      public void include(String path) throws jakarta.servlet.ServletException, IOException
      Specified by:
      include in class jakarta.servlet.jsp.PageContext
      Throws:
      jakarta.servlet.ServletException
      IOException
    • include

      public void include(String path, boolean flush) throws jakarta.servlet.ServletException, IOException
      Specified by:
      include in class jakarta.servlet.jsp.PageContext
      Throws:
      jakarta.servlet.ServletException
      IOException
    • getContentAsByteArray

      public byte[] getContentAsByteArray()
    • getContentAsString

      public String getContentAsString() throws UnsupportedEncodingException
      Throws:
      UnsupportedEncodingException
    • handlePageException

      public void handlePageException(Exception ex) throws jakarta.servlet.ServletException, IOException
      Specified by:
      handlePageException in class jakarta.servlet.jsp.PageContext
      Throws:
      jakarta.servlet.ServletException
      IOException
    • handlePageException

      public void handlePageException(Throwable ex) throws jakarta.servlet.ServletException, IOException
      Specified by:
      handlePageException in class jakarta.servlet.jsp.PageContext
      Throws:
      jakarta.servlet.ServletException
      IOException