> Markdown version of [Set Up Browserless Tests with Spring Boot](https://vaadin.com/docs/next/building-apps/testing/browserless/setup-spring-boot). Section index: [llms.txt](https://vaadin.com/docs/next/building-apps/llms.txt)

# Set Up Browserless Tests with Spring Boot

Use this guide for a Spring Boot application. Tests run with `@SpringBootTest`, and share a browserless application context that a test configuration defines as a Spring bean. For another application framework, choose its guide in [Browserless Testing](https://vaadin.com/docs/next/building-apps/testing/browserless.md#choose-your-framework).

## <a id="add-dependencies"></a>Add Dependencies

Assuming you’ve imported the Vaadin Bill-of-Materials (BOM) and have a Maven project, add the `browserless-test-spring` dependency with a `test` scope. Spring Boot’s test starter is also required; Spring Boot projects typically have it already:

```xml
<dependency>
    <groupId>com.vaadin</groupId>
    <artifactId>browserless-test-spring</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
```

> **Note:** If you’re upgrading a project that uses UI Unit Testing, remove the `vaadin-testbench-unit` or `vaadin-testbench-unit-junit5` dependency. See [Migrate UI Unit Tests to Browserless Tests](https://vaadin.com/docs/next/building-apps/testing/browserless/migrate-ui-unit-tests.md) for the other changes.

## <a id="first-browserless-test"></a>Create a Test

Views in Spring Boot applications typically get their services through dependency injection. `@SpringBootTest` starts the Spring application context for the test, and `SpringBrowserlessApplicationContext` creates views through it, so that views get the same beans as in the running application.

### <a id="shared-application-context"></a>Define the Application Context Once

Define the browserless application context as a bean in a test configuration class. Put the class in `src/test/java`, in the package of your `@SpringBootApplication` class, so that the component scan of `@SpringBootTest` finds it:

`BrowserlessContextConfiguration.java`

```java
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.vaadin.browserless.BrowserlessApplicationContext;
import com.vaadin.browserless.SpringBrowserlessApplicationContext;

@Configuration
public class BrowserlessContextConfiguration {

    @Bean
    BrowserlessApplicationContext browserlessApplicationContext(
            ApplicationContext applicationContext) {
        return SpringBrowserlessApplicationContext.create(applicationContext,
                Application.class); // (1)
    }
}
```

1. Creates the browserless application context on top of the Spring one. It scans the package of your `Application` class and its sub-packages for routes, which covers the views and layouts of the whole application.

Spring creates the bean once, when it starts the Spring application context, and every test that uses the same Spring application context shares it. Spring also closes it when it closes the Spring application context. If the application uses Spring Security, define the bean with `createSecured()` instead; see [Test View Access Control](https://vaadin.com/docs/next/building-apps/testing/browserless/test-view-access.md#spring-secured-context).

### <a id="write-the-test"></a>Write the Test

Given a simple view like this:

`HelloWorldView.java`

```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);
    }
}
```

A browserless test for it looks like this:

`HelloWorldViewTest.java`

```java
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.vaadin.browserless.BrowserlessApplicationContext;
import com.vaadin.browserless.BrowserlessUIContext;

@SpringBootTest
class HelloWorldViewTest {

    @Autowired
    private BrowserlessApplicationContext app; // (1)

    private BrowserlessUIContext window;

    @BeforeEach
    void openWindow() {
        window = app.newUser().newWindow(); // (2)
    }

    @AfterEach
    void closeUser() {
        window.getUser().close(); // (3)
    }

    @Test
    void setText_clickButton_notificationIsShown() {
        window.navigate(HelloWorldView.class); // (4)

        window.findTextField().withLabel("Your name").setValue("Test");
        window.findButton().withText("Say hello").click();

        Assertions.assertEquals("Hello Test",
                window.findNotification().getText());
    }

}
```

1. Spring injects the shared browserless application context.

2. Creates a user with its own Vaadin session, and opens a window for that user. The window shows the root route.

3. Closes the user and its window after the test. This ends the user’s session, and clears the Vaadin state, such as `UI.getCurrent()`, from the test thread, so that the next test starts clean.

4. The test drives the UI through the window: it finds components by their label or text, and types and clicks, as a user does.

Place the test in `src/test/java`, in the same Java package as the view. See [How a Browserless Test Works](https://vaadin.com/docs/next/building-apps/testing/browserless/test-user-interactions.md#how-a-browserless-test-works) for what each line of the test method does.

The tests share one Vaadin service, as the users of a running application do, and each test has a user and a session of its own. The application context is bound to the thread that creates it, so the tests must run on that thread, as they do with JUnit’s default sequential execution.

## <a id="run-the-test"></a>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.

## <a id="session-scoped-beans"></a>Access Session-Scoped Beans

A Spring test field is injected before the test opens its window, so before the user’s Vaadin session exists. If a test needs an application bean with `@VaadinSessionScope`, inject an `ObjectProvider` and resolve the bean after the window exists:

`CartViewSessionTest.java`

```java
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.vaadin.browserless.BrowserlessApplicationContext;
import com.vaadin.browserless.BrowserlessUIContext;

@SpringBootTest
class CartViewSessionTest {

    @Autowired
    private BrowserlessApplicationContext app;

    @Autowired
    private ObjectProvider<Cart> cartProvider;

    private BrowserlessUIContext window;

    @BeforeEach
    void openWindow() {
        window = app.newUser().newWindow();
    }

    @AfterEach
    void closeUser() {
        window.getUser().close();
    }

    @Test
    void addItem_cartContainsItem() {
        window.navigate(CartView.class);
        Cart cart = cartProvider.getObject();

        window.findButton().withText("Add to cart").click();

        Assertions.assertEquals(1, cart.getItems().size());
    }
}
```

This example assumes a session-scoped `Cart` service and a `CartView` whose **Add to cart** button adds an item to the cart. The provider resolves the bean for the user whose window is active — here the only window, which the preceding `navigate()` call used. Each test has a user of its own, so each test gets an empty cart. In a test with several users, call `activate()` on a window of the intended user before resolving the bean. The same approach works for a bean with Spring’s `@SessionScope`, which can also be injected directly. See [Spring Session-Scoped Beans](https://vaadin.com/docs/next/flow/testing/browserless/environment-differences.md#session-scoped-beans) for scope guarantees and other deferred-lookup options.

## <a id="context-per-test"></a>Create an Application Context for Each Test

Some tests need an application context of their own instead of the shared one:

- a test with several users, which closes all of them at once by closing its context; see [Test Multiple Users and Windows](https://vaadin.com/docs/next/building-apps/testing/browserless/test-multiple-users.md),

- a test class that needs different Vaadin settings, such as a feature flag; see [Configure a Browserless Test](https://vaadin.com/docs/next/building-apps/testing/browserless/configure-tests.md), and

- a test that selects a narrower Spring configuration with `@SpringJUnitConfig`, which doesn’t component-scan the shared bean; see [Use a Reduced Spring Application Context](https://vaadin.com/docs/next/building-apps/testing/browserless/speed-up-tests.md#using-a-reduced-application-context).

Such a test creates the context from the Spring application context before each test, and closes it afterwards:

```java
@Autowired
private ApplicationContext applicationContext;

private BrowserlessApplicationContext app;

@BeforeEach
void setUp() {
    app = SpringBrowserlessApplicationContext.create(applicationContext,
            CartView.class);
}

@AfterEach
void tearDown() {
    app.close();
}
```

Closing the application context closes every user and window created from it. The context scans only the packages of the classes passed to `create()`, and their sub-packages.

## <a id="base-class"></a>Extend a Base Class Instead

A test can also extend `SpringBrowserlessTest`, which creates the Vaadin environment with one user and one window before each test method, and closes it afterwards. The test then calls the window methods on itself:

`HelloWorldViewTest.java`

```java
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import com.vaadin.browserless.SpringBrowserlessTest;
import com.vaadin.flow.component.notification.Notification;

@SpringBootTest
class HelloWorldViewTest extends SpringBrowserlessTest {

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

}
```

The base class scans the whole classpath for routes, unless the test class has a `@ViewPackages` annotation. It applies Spring Security test annotations, such as `@WithMockUser`, to its user; see [Use Spring Security Test Annotations](https://vaadin.com/docs/next/building-apps/testing/browserless/test-view-access.md#spring-security-annotations). See [Base Classes](https://vaadin.com/docs/next/building-apps/testing/browserless.md#base-classes) for when to choose a base class.

## <a id="next-steps"></a>Next Steps

- [Test User Interactions](https://vaadin.com/docs/next/building-apps/testing/browserless/test-user-interactions.md) explains how the test works and how to cover more complex views.

- [Debug a Failing Browserless Test](https://vaadin.com/docs/next/building-apps/testing/browserless/debug-tests.md) helps when a test fails.

- [Application, User, and Window Contexts](https://vaadin.com/docs/next/flow/testing/browserless/multi-user.md) describes the context API in detail.

- [Test Environment and Lifecycle](https://vaadin.com/docs/next/flow/testing/browserless/test-environment.md) describes the environment lifecycle and the navigation API.

`7F423DA0-1C41-44BA-B832-55C269FA9311`
