Testing

Unit testing with Karibu-Testing

Karibu-Testing provides functionality to write browserless server-side JUnit Tests for Vaadin applications. This allows calls such as UI.getCurrent() in a regular JUnit test by mocking VaadinSession, CurrentRequest and others.

linkki provides an extension of Karibu Testing.

Maven Dependency

<dependency>
    <groupId>org.linkki-framework</groupId>
    <artifactId>linkki-vaadin-flow-test</artifactId>
</dependency>

Mock Vaadin setup with KaribuUIExtension

While some of the UI components created with Vaadin can be tested easily in a regular unit test, the setup gets difficult when dialogs, locales or routing are involved.

Karibu makes it easier by the class MockVaadinUI which not only set up a UI instance along with the session, but also can handle routes.

The linkki Karibu module further simplify the set-up with the JUnit extension KaribuUIExtension.

Simple set-up: KaribuUIExtension with @ExtendWith

In the most simple use case, this class can be directly annotated on the test class with @ExtendWith(KaribuUIExtension.class). This will make sure that the Vaadin UI is set up in beforeEach and is cleaned up properly in afterEach.

Additionally, a test using KaribuUIExtension can be annotated with @WithLocale to define the UI’s locale, defaulting to german.

+ .KaribuUIExtension with @ExtendWith

@ExtendWith(KaribuUIExtension.class)
class SimpleTestClass {

    @Test
    void testUI() {
        assertThat(UI.getCurrent()).isNotNull();
    }

}
Complex configuration of KaribuUIExtension

KaribuUIExtension can also be used for more complex configurations. It provides factory methods with which a configuration can be passed. The configuration can be for exampled used to:

  • add routes

  • add error routes

  • add instances for parent layouts

  • define whether the deployment is in production mode or

  • define the locale.

To use the configuration, the KaribuUIExtension must be used with @RegisterExtension.

KaribuUIExtension with complex configuration
    class RegisterExtensionWithConfiguration {

        @RegisterExtension
        static KaribuUIExtension extension = KaribuUIExtension
                .withConfiguration(withDefaults()
                        .setProductionMode(true)
                        .addRoute(MyView.class, () -> myView)
                        .addRoutes(ViewWithParentLayout.class,
                                   ViewWithException.class)
                        .addErrorRoutes(ErrorPage.class)
                        .addInstance(ParentLayout.class, ParentLayout::new));
        ...
    }
Locale

The locale can be either set with the annotation @WithLocale, or with the method setLocale in the configuration.

KaribuUIExtension with @ExtendWith
@WithLocale("en")
@ExtendWith(KaribuUIExtension.class)
class SimpleTestClassWithLocaleEn {

    @Test
    void testLocale() {
        assertThat(UI.getCurrent().getLocale()).isEqualTo(Locale.ENGLISH);
    }

}

Finding elements and writing assertions

Karibu provides a class LocatorJ that provides many useful methods to find elements, most notably _find and _get. In addition, the class also provides several methods for assertions such as _assertOne which provides a very useful failure message.

The linkki extension for Karibu testing offers additional functionalities in the utility class KaribuUtils. The functionalities include

  • methods for printing out any component as String. This can be used to assert that a certain text is or is not displayed anywhere within the given component, disregarding the placement or the actual component that is used

  • methods for handling value change events in input fields

  • methods for handling grids

  • methods for handling layouts created with a PMO

  • methods for handling Notification components that are created with NotificationUtil

  • methods for handling OkCancelDialog

  • support for push UI