com.vaadin.testbench.unit.

Class UIUnitTest

java.lang.Object
com.vaadin.testbench.unit.BaseUIUnitTest
com.vaadin.testbench.unit.UIUnitTest

All Implemented Interfaces:

TesterWrappers

Direct Known Subclasses:

SpringUIUnitTest

public abstract class UIUnitTest extends BaseUIUnitTest implements TesterWrappers

Base JUnit 5 class for UI unit tests. The class automatically scans 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

 {@code
 @ViewPackages(classes = {CartView.class, CheckoutView.class})
 class CartViewTest extends UIUnitTest {
 }

 @ViewPackages(packages = {"com.example.shop.cart", "com.example.security"})
 class CartViewTest extends UIUnitTest {
 }

 @ViewPackages(
    classes = {CartView.class, CheckoutView.class},
    packages = {"com.example.security"}
 )
 class CartViewTest extends UIUnitTest {
 }
 </pre>

 Set up of Vaadin environment is performed before each test by {@link
 #initVaadinEnvironment()} method, and will be executed before
 {@code @BeforeEach} methods defined in subclasses. At the same way, cleanup
 tasks operated by {@link #cleanVaadinEnvironment()} are executed after each
 test, and after all {@code @AfterEach} annotated methods in subclasses.

 Usually, it is not necessary to override {@link #initVaadinEnvironment()} or
 {@link #cleanVaadinEnvironment()} methods, but if this is done it is
 mandatory to add the {@code @BeforeEach} and {@code @AfterEach} annotations
 in the subclass, in order to have hooks handled by testing framework.

 A use case for overriding {@link #initVaadinEnvironment()} is to provide
 custom Flow service implementations supported by {@link
 com.vaadin.flow.di.Lookup} SPI. Implementations can be provided overriding
 {@link #initVaadinEnvironment()} and passing to super implementation the
 service classes that should be initialized during setup.

 <pre>
 {@code
 &#64;BeforeEach
 &#64;Override
 void initVaadinEnvironment() {
     super.initVaadinEnvironment(CustomInstantiatorFactory.class);
 }
 }
 </pre>
 <p/>
 To get a graphical ascii representation of the UI tree on failure add the
 annotation {@code @ExtendWith(TreeOnFailureExtension.class)} to the test
 class.

 @see ViewPackages

  • Constructor Details

    • UIUnitTest

      public UIUnitTest()
  • Method Details

    • initVaadinEnvironment

      @BeforeEach protected void initVaadinEnvironment()

      Description copied from class: BaseUIUnitTest

      Create mocked Vaadin core obects, such as session, servlet populated with Routes, UI etc. for testing and find testers for the components.

      Overrides:

      initVaadinEnvironment in class BaseUIUnitTest

    • cleanVaadinEnvironment

      @AfterEach protected void cleanVaadinEnvironment()

      Description copied from class: BaseUIUnitTest

      Tears down mocked Vaadin.

      Overrides:

      cleanVaadinEnvironment in class BaseUIUnitTest

    • testingEngine

      protected final String testingEngine()

      Description copied from class: BaseUIUnitTest

      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.

      Specified by:

      testingEngine in class BaseUIUnitTest

      Returns:

      name of the Test Engine.