Class MockUIExtension

java.lang.Object
com.vaadin.tests.MockUIExtension
All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension

public class MockUIExtension extends Object implements org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback
JUnit extension to set up a UI and VaadinSession for testing.

Usage:

 @RegisterExtension
 MockUIExtension ui = new MockUIExtension();
 
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(com.vaadin.flow.component.Component component)
    Add a component to the UI.
    void
    afterEach(org.junit.jupiter.api.extension.ExtensionContext context)
     
    void
    beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
     
    void
    Clears the current the UI.
    List<com.vaadin.flow.component.internal.PendingJavaScriptInvocation>
    Dump the pending JavaScript invocations from the UI internals.
    void
    Simulate client communication by running pending executions and collecting changes.
    com.vaadin.flow.server.VaadinService
    Get the VaadinService instance that is set up by this extension.
    com.vaadin.flow.server.VaadinSession
    Get the VaadinSession instance that is set up by this extension.
    com.vaadin.flow.component.UI
    Get the UI instance that is set up by this extension.
    void
    remove(com.vaadin.flow.component.Component component)
    Remove a component from the UI.
    void
    Remove all components from the UI.
    void
    Replaces the current UI with a new instance.
    void
    setLocale(Locale locale)
    Set the locale of the UI.

    Methods inherited from class java.lang.Object

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

    • MockUIExtension

      public MockUIExtension()
  • Method Details

    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
    • afterEach

      public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
    • getUI

      public com.vaadin.flow.component.UI getUI()
      Get the UI instance that is set up by this extension.
      Returns:
      the UI instance
    • clearUI

      public void clearUI()
      Clears the current the UI.
    • replaceUI

      public void replaceUI()
      Replaces the current UI with a new instance.
    • getSession

      public com.vaadin.flow.server.VaadinSession getSession()
      Get the VaadinSession instance that is set up by this extension.

      Note that this returns a Mockito spy, so it supports stubbing and verification.

      Returns:
      the VaadinSession instance
    • getService

      public com.vaadin.flow.server.VaadinService getService()
      Get the VaadinService instance that is set up by this extension.

      Note that this returns a Mockito mock, so it supports stubbing and verification.

      Returns:
      the VaadinService instance
    • add

      public void add(com.vaadin.flow.component.Component component)
      Add a component to the UI. Delegates to UI.add(Component...) for convenience.
      Parameters:
      component - the component to add
    • remove

      public void remove(com.vaadin.flow.component.Component component)
      Remove a component from the UI. Delegates to HasComponents.remove(Component...) for convenience.
      Parameters:
      component - the component to remove
    • removeAll

      public void removeAll()
      Remove all components from the UI. Delegates to HasComponents.removeAll() for convenience.
    • setLocale

      public void setLocale(Locale locale)
      Set the locale of the UI. Delegates to UI.setLocale(Locale) for convenience.
      Parameters:
      locale - the locale to set
    • fakeClientCommunication

      public void fakeClientCommunication()
      Simulate client communication by running pending executions and collecting changes.
    • dumpPendingJavaScriptInvocations

      public List<com.vaadin.flow.component.internal.PendingJavaScriptInvocation> dumpPendingJavaScriptInvocations()
      Dump the pending JavaScript invocations from the UI internals.
      Returns:
      a list of pending JavaScript invocations