Docs

Documentation versions (currently viewingVaadin 25)
Documentation translations (currently viewingEnglish)

Set Up Browserless Tests with Quarkus

Configure Quarkus browserless tests, create a test class, and substitute services using test profiles.

Use this guide for a Quarkus Flow application. Tests extend QuarkusBrowserlessTest and run with @QuarkusTest. For a Java EE application that uses Vaadin CDI and Weld, use the separate CDI setup.

Add Dependencies

With the Vaadin BOM imported, add these test dependencies to your pom.xml file:

Source code
pom.xml
<dependency>
    <groupId>com.vaadin</groupId>
    <artifactId>browserless-test-junit6</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.vaadin</groupId>
    <artifactId>browserless-test-quarkus</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-junit</artifactId>
    <scope>test</scope>
</dependency>

Create a Test

Create this view:

Source code
HelloWorldView.java
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.Route;

@Route("")
public class HelloWorldView extends HorizontalLayout {

    TextField name;
    Button sayHello;

    public HelloWorldView() {
        name = new TextField("Your name");
        sayHello = new Button("Say hello");
        sayHello.addClickListener(e -> {
            Notification.show("Hello " + name.getValue());
        });
        add(name, sayHello);
    }
}

Create a test class that extends QuarkusBrowserlessTest, in the same Java package as the view under src/test/java:

Source code
HelloWorldViewTest.java
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import com.vaadin.browserless.quarkus.QuarkusBrowserlessTest;
import com.vaadin.flow.component.notification.Notification;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
class HelloWorldViewTest extends QuarkusBrowserlessTest {
    @Test
    public void setText_clickButton_notificationIsShown() {
        final HelloWorldView helloView = navigate(HelloWorldView.class);

        test(helloView.name).setValue("Test");
        test(helloView.sayHello).click();

        Notification notification = find(Notification.class).single();
        Assertions.assertEquals("Hello Test", test(notification).getText());
    }
}

See How a Browserless Test Works for what each line of the test does.

Note
@QuarkusTest starts the application and its HTTP server, which browserless tests don’t use. The QuarkusBrowserlessTest tests still run in a mocked Vaadin environment.

Run the Test

Run the test from your IDE or with mvn test. The test passes when entering a name and clicking the button shows the expected notification.

Replace a Service with a Test Profile

Annotate a test with @TestProfile to run it with a specific test configuration. A test profile can, for example, override application configuration, enable bean alternatives, and provide custom test resources. See the Quarkus Testing Profiles documentation for all options.

This example replaces a GreetingService with a test implementation. It assumes a WelcomeView that shows the result of greetingService.greet("Quarkus") in a span stored in its greeting field. The test implementation is a CDI bean annotated with @Alternative:

Source code
MockService.java
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Alternative;

@Alternative
@ApplicationScoped
public class MockService implements GreetingService {
    @Override
    public String greet(String name) {
        return "Hello " + name + " from the mock service";
    }
}

The profile enables the alternative and also overrides the app.some.config property:

Source code
MockServiceProfile.java
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import io.quarkus.test.junit.QuarkusTestProfile;

public class MockServiceProfile implements QuarkusTestProfile {

    @Override
    public Map<String, String> getConfigOverrides() {
        return Collections.singletonMap("app.some.config","value");
    }

    @Override
    public Set<Class<?>> getEnabledAlternatives() {
        return Collections.singleton(MockService.class);
    }
}

A test that uses the profile gets the alternative injected into the view:

Source code
MockServiceViewTest.java
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import com.vaadin.browserless.quarkus.QuarkusBrowserlessTest;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;

@QuarkusTest
@TestProfile(MockServiceProfile.class)
class MockServiceViewTest extends QuarkusBrowserlessTest {
    @Test
    void view_usesMockService() {
        WelcomeView view = navigate(WelcomeView.class);
        Assertions.assertEquals("Hello Quarkus from the mock service",
                view.greeting.getText());
    }
}

Next Steps

09A26994-97AB-4877-AEB1-717BE02E348D