Class MockUIExtension
- All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterEachCallback,org.junit.jupiter.api.extension.BeforeEachCallback,org.junit.jupiter.api.extension.Extension
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
FieldsModifier and TypeFieldDescriptionstatic final StringThe system property with the Content Security Policy mode that every UI is set up with,offorstrict. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(com.vaadin.flow.component.Component component) Add a component to the UI.voidafterEach(org.junit.jupiter.api.extension.ExtensionContext context) voidbeforeEach(org.junit.jupiter.api.extension.ExtensionContext context) voidclearUI()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 withexecuteJs(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 withcallJsFunctionfor the given function.List<com.vaadin.flow.component.internal.PendingJavaScriptInvocation> Dump the pending JavaScript invocations from the UI internals.voidSimulate client communication by running pending executions and collecting changes.com.vaadin.flow.server.VaadinContextGet the VaadinContext instance that the mockedVaadinServicereturns.com.vaadin.flow.server.VaadinServiceGet the VaadinService instance that is set up by this extension.com.vaadin.flow.server.VaadinSessionGet the VaadinSession instance that is set up by this extension.com.vaadin.flow.component.UIgetUI()Get the UI instance that is set up by this extension.voidremove(com.vaadin.flow.component.Component component) Remove a component from the UI.voidRemove all components from the UI.voidReplaces the current UI with a new instance.voidsetCspMode(com.vaadin.flow.server.CspMode mode) Sets the Content Security Policy mode that the mockedVaadinServicereports, and makes it the current service, which is where the mode is read from.voidSet the locale of the UI.voidsetUp()Sets up a UI for testing.voidtearDown()Tears down a UI that was previously set up.
-
Field Details
-
CSP_MODE_PROPERTY
The system property with the Content Security Policy mode that every UI is set up with,offorstrict. Set from thecsp.modeMaven 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:
beforeEachin interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
-
afterEach
public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
afterEachin interfaceorg.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 usingDynamicTest. -
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 usingDynamicTest. -
setCspMode
public void setCspMode(com.vaadin.flow.server.CspMode mode) Sets the Content Security Policy mode that the mockedVaadinServicereports, 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 asElement.executeJs(String, Object...), throws aCspViolationException. A test can use this to check that a component only runs declared JavaScript, seeElement.executeJs(Class). The current service is cleared again when the UI is torn down.- Parameters:
mode- the mode to use, notnull
-
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 mockedVaadinServicereturns.Note that this returns a Mockito mock, so it supports stubbing and verification. An
ApplicationConfigurationattribute 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 toUI.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 toHasComponentsOfType.remove(Component...)for convenience.- Parameters:
component- the component to remove
-
removeAll
public void removeAll()Remove all components from the UI. Delegates toHasComponentsOfType.removeAll()for convenience. -
setLocale
Set the locale of the UI. Delegates toUI.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 withexecuteJs(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 UIdefinitionType- 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 withcallJsFunctionfor 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, usedumpJsCalls(Element, Class)withElement.CallFunctionJs.Like
dumpPendingJavaScriptInvocations(), this clears all pending invocations, including those that are not returned.- Parameters:
owner- the element that the function was called onfunctionName- the name of the called function- Returns:
- a list of the matching calls, in the order they were made
-