Class MockVaadinHelper

java.lang.Object
com.vaadin.browserless.mocks.MockVaadinHelper

public final class MockVaadinHelper extends Object
Builds the ServletContext the mocked environment runs in.

A deployed application gets its context from the servlet container, which runs Vaadin's ServletContainerInitializers to install the Lookup. There is no container here, so createMockContext(Set) creates a MockContext and runs the lookup initializer over it by hand, seeding it with Vaadin's own services plus whatever the test asked for. Everything downstream — the instantiator, the application configuration, the route registry — is then resolved through that Lookup, exactly as in a real deployment.

It also papers over the one thing a component module cannot provide: mockFlowBuildInfo(VaadinServlet) writes a dummy token file when there is no flow-build-info.json on the classpath, which Vaadin's mode detection would otherwise reject.

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

  • Method Details

    • mockFlowBuildInfo

      public static void mockFlowBuildInfo(VaadinServlet servlet)
      Makes sure the servlet can read a flow-build-info.json, writing a dummy one when the classpath has none.

      Without it DeploymentConfigurationFactory.verifyMode() rejects the environment, which is what happens when the tests run inside a Vaadin component module rather than an application.

      Parameters:
      servlet - the servlet whose context to configure
    • createMockContext

      public static jakarta.servlet.ServletContext createMockContext()
      Creates a context holding a Lookup with Vaadin's own services.
      Returns:
      the new context
    • createMockContext

      public static jakarta.servlet.ServletContext createMockContext(Set<Class<?>> lookupServices)
      Creates a context holding a Lookup with Vaadin's own services plus the given ones.
      Parameters:
      lookupServices - extra service implementations to register; one supplied here wins over the default for the same service interface
      Returns:
      the new context
    • createMockVaadinContext

      public static VaadinContext createMockVaadinContext()
      Creates a VaadinContext wrapping a fresh createMockContext().
      Returns:
      the new context
    • getTokenFileFromClassloader

      public static tools.jackson.databind.node.ObjectNode getTokenFileFromClassloader()
      Reads flow-build-info.json from the classpath the same way Vaadin does, through DefaultApplicationConfigurationFactory.
      Returns:
      the parsed token file, or null if the classpath has none
    • lookup

      public static Object lookup(VaadinContext ctx, Class<?> clazz)
      Looks a service up in the context's Lookup.
      Parameters:
      ctx - the context to look in; it must already hold a Lookup
      clazz - the service interface to resolve
      Returns:
      the service, or null if none is registered
      Throws:
      IllegalStateException - if the context holds no Lookup