Class MockVaadinHelper
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThe lookup initializer of the mocked environment. -
Method Summary
Modifier and TypeMethodDescriptionstatic jakarta.servlet.ServletContextCreates a context holding aLookupwith Vaadin's own services.static jakarta.servlet.ServletContextcreateMockContext(Set<Class<?>> lookupServices) Creates a context holding aLookupwith Vaadin's own services plus the given ones.static VaadinContextCreates aVaadinContextwrapping a freshcreateMockContext().static tools.jackson.databind.node.ObjectNodeReadsflow-build-info.jsonfrom the classpath the same way Vaadin does, throughDefaultApplicationConfigurationFactory.static Objectlookup(VaadinContext ctx, Class<?> clazz) Looks a service up in the context'sLookup.static voidmockFlowBuildInfo(VaadinServlet servlet) Makes sure the servlet can read aflow-build-info.json, writing a dummy one when the classpath has none.
-
Method Details
-
mockFlowBuildInfo
Makes sure the servlet can read aflow-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 aLookupwith Vaadin's own services.- Returns:
- the new context
-
createMockContext
Creates a context holding aLookupwith 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
Creates aVaadinContextwrapping a freshcreateMockContext().- Returns:
- the new context
-
getTokenFileFromClassloader
public static tools.jackson.databind.node.ObjectNode getTokenFileFromClassloader()Readsflow-build-info.jsonfrom the classpath the same way Vaadin does, throughDefaultApplicationConfigurationFactory.- Returns:
- the parsed token file, or
nullif the classpath has none
-
lookup
Looks a service up in the context'sLookup.- Parameters:
ctx- the context to look in; it must already hold aLookupclazz- the service interface to resolve- Returns:
- the service, or
nullif none is registered - Throws:
IllegalStateException- if the context holds noLookup
-