Class BaseUIUnitTest

java.lang.Object
com.vaadin.testbench.unit.BaseUIUnitTest
Direct Known Subclasses:
UIUnit4Test, UIUnitTest

public abstract class BaseUIUnitTest extends Object
Base class for UI unit tests. Provides methods to set up and clean a mocked Vaadin environment. The class allows scan classpath for routes and error views. Subclasses should typically restrict classpath scanning to a specific packages for faster bootstrap, by using ViewPackages annotation. If the annotation is not present a full classpath scan is performed For internal use only. May be renamed or removed in a future release.
See Also:
  • Field Details

  • Constructor Details

    • BaseUIUnitTest

      public BaseUIUnitTest()
  • Method Details

    • discoverRoutes

      protected com.vaadin.testbench.unit.internal.Routes discoverRoutes()
    • discoverRoutes

      protected static com.vaadin.testbench.unit.internal.Routes discoverRoutes(Set<String> packageNames)
      Discover and return Routes for mocked Vaadin core system.
      Returns:
      Routes
      See Also:
    • initVaadinEnvironment

      protected void initVaadinEnvironment()
      Create mocked Vaadin core obects, such as session, servlet populated with Routes, UI etc. for testing and find testers for the components.
    • initSignalsSupport

      protected void initSignalsSupport()
    • scanTesters

      protected void scanTesters()
      Scan testers and populate testers map with them. The test method can find appropriate test based on testers map.
      See Also:
    • cleanVaadinEnvironment

      protected void cleanVaadinEnvironment()
      Tears down mocked Vaadin.
    • lookupServices

      protected Set<Class<?>> lookupServices()
      Gets the services implementations to be used to initialized Vaadin Lookup. Default implementation returns an empty Set. Override this method to provide custom Vaadin services, such as InstantiatorFactory, ResourceProvider, etc.
      Returns:
      set of services implementation classes, never null.
    • fireShortcut

      public void fireShortcut(Key key, KeyModifier... modifiers)
      Simulates a keyboard shortcut performed on the browser.
      Parameters:
      key - Primary key of the shortcut. This must not be a KeyModifier.
      modifiers - Key modifiers. Can be empty.
    • getCurrentView

      public HasElement getCurrentView()
      Get the current view instance that is shown on the ui.
      Returns:
      current view
    • test

      public <T extends ComponentTester<Y>, Y extends Component> T test(Y component)
      Wrap component with ComponentTester best matching component type.
      Type Parameters:
      T - tester type
      Y - component type
      Parameters:
      component - component to get test wrapper for
      Returns:
      component in wrapper with test helpers
    • test

      public <T extends ComponentTester<Y>, Y extends Component> T test(Class<T> tester, Y component)
      Wrap component in given ComponentTester.
      Type Parameters:
      T - tester type
      Y - component type
      Parameters:
      tester - test wrapper to use
      component - component to wrap
      Returns:
      initialized test wrapper for component
    • $

      public <T extends Component> ComponentQuery<T> $(Class<T> componentType)
      Gets a query object for finding a component inside the UI
      Type Parameters:
      T - the type of the component(s) to search for
      Parameters:
      componentType - the type of the component(s) to search for
      Returns:
      a query object for finding components
    • $

      public <T extends Component> ComponentQuery<T> $(Class<T> componentType, Component fromThis)
      Gets a query object for finding a component nested inside the given component.
      Type Parameters:
      T - the type of the component(s) to search for
      Parameters:
      componentType - the type of the component(s) to search for
      fromThis - component used as starting element for search.
      Returns:
      a query object for finding components
    • $view

      public <T extends Component> ComponentQuery<T> $view(Class<T> componentType)
      Gets a query object for finding a component inside the current view
      Type Parameters:
      T - the type of the component(s) to search for
      Parameters:
      componentType - the type of the component(s) to search for
      Returns:
      a query object for finding components
    • roundTrip

      protected static void roundTrip()
      Simulates a server round-trip, flushing pending component changes.
    • runPendingSignalsTasks

      protected final boolean runPendingSignalsTasks()
      Processes all pending Signals tasks with a default max wait time of 100 milliseconds. This is a convenience method for tests that need to wait for asynchronous Signal effects to complete.

      When Signals are triggered from background threads or non-UI contexts, their effects are enqueued to simulate asynchronous processing. This method allows tests to flush and execute all such pending tasks synchronously, ensuring deterministic behavior in unit tests.

      If any VaadinSession lock is held by the current thread, it is temporarily released during the wait to allow background threads to acquire the lock and enqueue tasks.

      If Signals support is not enabled (via the FLOW_FULLSTACK_SIGNALS feature flag), this method does nothing.

      Returns:
      true if any pending Signals tasks were processed.
      See Also:
    • runPendingSignalsTasks

      protected final boolean runPendingSignalsTasks(long maxWaitTime, TimeUnit unit)
      Processes all pending Signals tasks, waiting up to the specified timeout for tasks to arrive. This method is essential for testing asynchronous Signal effects triggered from background threads or non-UI contexts.

      When Signals are triggered from background threads or non-UI contexts, their effects are enqueued to simulate asynchronous processing. This method allows tests to flush and execute all such pending tasks synchronously, ensuring deterministic behavior in unit tests.

      The timeout applies only to waiting for the first task to arrive. Once the first task is found, all remaining tasks in the queue are processed immediately without additional waiting. If any VaadinSession lock is held by the current thread, it is temporarily released during the wait to allow background threads to acquire the lock and enqueue tasks.

      If Signals support is not enabled (via the FLOW_FULLSTACK_SIGNALS feature flag), this method does nothing.

      Parameters:
      maxWaitTime - the maximum time to wait for the first task to arrive in the given time unit. If <= 0, returns immediately if no tasks are available.
      unit - the time unit of the timeout value
      Returns:
      true if any pending Signals tasks were processed.
      See Also:
      • TestSignalEnvironment.runPendingTasks(long, TimeUnit)
    • testingEngine

      protected abstract String testingEngine()
      Gets the name of the Test Engine that is able to run the base class implementation. The Test Engine name is reported in the exception thrown when the Vaadin environment is not set up correctly.
      Returns:
      name of the Test Engine.