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 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:
  • 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 DeploymentConfiguration createDeploymentConfiguration() throws jakarta.servlet.ServletException
      Description copied from class: VaadinServlet
      Creates a deployment configuration to be used for the creation of a VaadinService. Intended to be used by dependency injection frameworks.
      Overrides:
      createDeploymentConfiguration in class VaadinServlet
      Returns:
      the created deployment configuration
      Throws:
      jakarta.servlet.ServletException - if creating the deployment configuration fails
    • createServletService

      protected VaadinServletService createServletService(DeploymentConfiguration deploymentConfiguration) throws ServiceException
      Description copied from class: VaadinServlet
      Creates a vaadin servlet service.
      Overrides:
      createServletService in class VaadinServlet
      Parameters:
      deploymentConfiguration - the deployment configuration to be used
      Returns:
      the created vaadin servlet service
      Throws:
      ServiceException - if creating the vaadin servlet service fails
    • createWebBrowser

      public static WebBrowser createWebBrowser(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 VaadinSession createVaadinSession(VaadinService service, 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(VaadinService service, 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