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();
 

In a module whose csp.mode Maven property is strict, the UI is set up with CspMode.STRICT, see setCspMode(CspMode). The mode reaches the tests as the "vaadin.test.csp" system property.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The system property with the Content Security Policy mode that every UI is set up with, off or strict.
  • 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.js.JsCall>
    dumpJsCalls(com.vaadin.flow.dom.Element owner, Class<?> definitionType)
    Dump the pending JavaScript invocations from the UI internals, and get the calls among them that were made on the given element through the given JavaScript definition with executeJs(Class).
    List<com.vaadin.flow.js.JsCall>
    dumpJsCalls(com.vaadin.flow.dom.Element owner, String functionName)
    Dump the pending JavaScript invocations from the UI internals, and get the calls among them that were made on the given element with callJsFunction for the given function.
    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.VaadinContext
    Get the VaadinContext instance that the mocked VaadinService returns.
    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
    setCspMode(com.vaadin.flow.server.CspMode mode)
    Sets the Content Security Policy mode that the mocked VaadinService reports, and makes it the current service, which is where the mode is read from.
    void
    setLocale(Locale locale)
    Set the locale of the UI.
    void
    Sets up a UI for testing.
    void
    Tears down a UI that was previously set up.

    Methods inherited from class java.lang.Object

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

    • CSP_MODE_PROPERTY

      public static final String CSP_MODE_PROPERTY
      The system property with the Content Security Policy mode that every UI is set up with, off or strict. Set from the csp.mode Maven property of the module.
      See Also:
  • 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
    • setUp

      public void setUp()
      Sets up a UI for testing. Only use this method directly in contexts where registering the extension is not possible, for example when using DynamicTest.
    • tearDown

      public void tearDown()
      Tears down a UI that was previously set up. Only use this method directly in contexts where registering the extension is not possible, for example when using DynamicTest.
    • setCspMode

      public void setCspMode(com.vaadin.flow.server.CspMode mode)
      Sets the Content Security Policy mode that the mocked VaadinService reports, and makes it the current service, which is where the mode is read from.

      With CspMode.STRICT, every API that sends JavaScript to the browser as a string, such as Element.executeJs(String, Object...), throws a CspViolationException. A test can use this to check that a component only runs declared JavaScript, see Element.executeJs(Class). The current service is cleared again when the UI is torn down.

      Parameters:
      mode - the mode to use, not null
    • 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
    • getContext

      public com.vaadin.flow.server.VaadinContext getContext()
      Get the VaadinContext instance that the mocked VaadinService returns.

      Note that this returns a Mockito mock, so it supports stubbing and verification. An ApplicationConfiguration attribute is stubbed on it already.

      Returns:
      the VaadinContext 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 HasComponentsOfType.remove(Component...) for convenience.
      Parameters:
      component - the component to remove
    • removeAll

      public void removeAll()
      Remove all components from the UI. Delegates to HasComponentsOfType.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
    • dumpJsCalls

      public List<com.vaadin.flow.js.JsCall> dumpJsCalls(com.vaadin.flow.dom.Element owner, Class<?> definitionType)
      Dump the pending JavaScript invocations from the UI internals, and get the calls among them that were made on the given element through the given JavaScript definition with executeJs(Class).

      Like dumpPendingJavaScriptInvocations(), this clears all pending invocations, including those that are not returned.

      Parameters:
      owner - the element that the calls were made on, use the UI element for calls made on the UI
      definitionType - the JavaScript definition interface of the calls
      Returns:
      a list of the matching calls, in the order they were made
    • dumpJsCalls

      public List<com.vaadin.flow.js.JsCall> dumpJsCalls(com.vaadin.flow.dom.Element owner, String functionName)
      Dump the pending JavaScript invocations from the UI internals, and get the calls among them that were made on the given element with callJsFunction for the given function.

      JsCall.flattenArguments() of a returned call is the function name followed by the arguments of the function. To get the calls of all functions, use dumpJsCalls(Element, Class) with Element.CallFunctionJs.

      Like dumpPendingJavaScriptInvocations(), this clears all pending invocations, including those that are not returned.

      Parameters:
      owner - the element that the function was called on
      functionName - the name of the called function
      Returns:
      a list of the matching calls, in the order they were made