Class MockVaadinServlet

java.lang.Object
jakarta.servlet.GenericServlet
jakarta.servlet.http.HttpServlet
com.vaadin.flow.server.VaadinServlet
com.vaadin.browserless.mocks.MockVaadinServlet
All Implemented Interfaces:
jakarta.servlet.Servlet, jakarta.servlet.ServletConfig, Serializable

public class MockVaadinServlet extends com.vaadin.flow.server.VaadinServlet
The VaadinServlet of the mocked environment.

It registers the routes it was given into the route registry, which a deployment would otherwise populate by classpath scanning at startup, and it creates a MockService in place of a vanilla VaadinServletService.

It also carries the static helpers that reach into Flow for things a test cannot construct directly — a WebBrowser or a VaadinSession.

To use a custom servlet instead of this one, pass it to MockVaadin.setup(com.vaadin.browserless.internal.Routes, com.vaadin.browserless.internal.UIFactory, java.util.Set<? extends java.lang.Class<?>>, com.vaadin.browserless.BrowserlessConfiguration).

For internal use only. May be renamed or removed in a future release.

See Also:
  • Field Summary

    Fields inherited from class com.vaadin.flow.server.VaadinServlet

    INTERNAL_VAADIN_SERVLET_VITE_DEV_MODE_FRONTEND_PATH

    Fields inherited from class jakarta.servlet.http.HttpServlet

    LEGACY_DO_HEAD
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a servlet with no routes, producing MockedUI instances.
    Creates a servlet serving the given routes, producing MockedUI instances.
    MockVaadinServlet(Routes routes, UIFactory uiFactory)
    Creates a servlet serving the given routes, producing UIs from the given factory.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    applySessionLockCheckStrategy(com.vaadin.flow.server.VaadinService service, com.vaadin.flow.server.VaadinSession session)
    Applies the service's session lock check strategy to the given session, as Flow does for a session it creates itself.
    protected com.vaadin.flow.function.DeploymentConfiguration
     
    protected com.vaadin.flow.server.VaadinServletService
    createServletService(com.vaadin.flow.function.DeploymentConfiguration deploymentConfiguration)
     
    static com.vaadin.flow.server.VaadinSession
    createVaadinSession(com.vaadin.flow.server.VaadinService service, com.vaadin.flow.server.VaadinRequest request)
    Creates a session by calling the protected VaadinService.createVaadinSession(VaadinRequest).
    static com.vaadin.flow.server.WebBrowser
    createWebBrowser(com.vaadin.flow.server.VaadinRequest request)
    Creates the browser details the session reports.
    Returns the routes this servlet registers.
    Returns the factory creating the UI of the session that replaces a closed session of this servlet.

    Methods inherited from class com.vaadin.flow.server.VaadinServlet

    createDeploymentConfiguration, createServletService, createStaticFileHandler, createVaadinRequest, destroy, getCurrent, getFrontendMapping, getLastPathParameter, getService, getServletConfig, handleContextOrServletRootWithoutSlash, init, serveStaticOrWebJarRequest, service, servletInitialized, whenFrontendMappingAvailable

    Methods inherited from class jakarta.servlet.http.HttpServlet

    doDelete, doGet, doHead, doOptions, doPatch, doPost, doPut, doTrace, getLastModified, isSensitiveHeader, service

    Methods inherited from class jakarta.servlet.GenericServlet

    getInitParameter, getInitParameterNames, getServletContext, getServletInfo, getServletName, init, log, log

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MockVaadinServlet

      public MockVaadinServlet()
      Creates a servlet with no routes, producing MockedUI instances.
    • MockVaadinServlet

      public MockVaadinServlet(Routes routes)
      Creates a servlet serving the given routes, producing MockedUI instances.
      Parameters:
      routes - the routes to register
    • MockVaadinServlet

      public MockVaadinServlet(Routes routes, UIFactory uiFactory)
      Creates a servlet serving the given routes, producing UIs from the given factory.
      Parameters:
      routes - the routes to register
      uiFactory - creates the UI of the session that replaces a closed session; it must return a fresh instance every time
  • Method Details

    • getRoutes

      public Routes getRoutes()
      Returns the routes this servlet registers.
      Returns:
      the routes
    • getUiFactory

      public UIFactory getUiFactory()
      Returns the factory creating the UI of the session that replaces a closed session of this servlet.
      Returns:
      the UI factory
    • createDeploymentConfiguration

      protected com.vaadin.flow.function.DeploymentConfiguration createDeploymentConfiguration() throws jakarta.servlet.ServletException
      Overrides:
      createDeploymentConfiguration in class com.vaadin.flow.server.VaadinServlet
      Throws:
      jakarta.servlet.ServletException
    • createServletService

      protected com.vaadin.flow.server.VaadinServletService createServletService(com.vaadin.flow.function.DeploymentConfiguration deploymentConfiguration) throws com.vaadin.flow.server.ServiceException
      Overrides:
      createServletService in class com.vaadin.flow.server.VaadinServlet
      Throws:
      com.vaadin.flow.server.ServiceException
    • createWebBrowser

      public static com.vaadin.flow.server.WebBrowser createWebBrowser(com.vaadin.flow.server.VaadinRequest request)
      Creates the browser details the session reports. They are derived from the request's User-Agent header.
      Parameters:
      request - the request the browser details are taken from
      Returns:
      the browser details
    • createVaadinSession

      public static com.vaadin.flow.server.VaadinSession createVaadinSession(com.vaadin.flow.server.VaadinService service, com.vaadin.flow.server.VaadinRequest request)
      Creates a session by calling the protected VaadinService.createVaadinSession(VaadinRequest). The session is only constructed: locking it and storing it in the HTTP session are left to the caller.
      Parameters:
      service - the service the session belongs to
      request - the request the session is created for
      Returns:
      the new session
    • applySessionLockCheckStrategy

      public static void applySessionLockCheckStrategy(com.vaadin.flow.server.VaadinService service, com.vaadin.flow.server.VaadinSession session)
      Applies the service's session lock check strategy to the given session, as Flow does for a session it creates itself.
      Parameters:
      service - the service the session belongs to
      session - the session to apply the strategy to