> 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 extend `SpringBrowserlessTest` and run with the Spring application context. 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>
```

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

Views in Spring Boot applications typically get their services through dependency injection. `SpringBrowserlessTest` creates views through the Spring application context, and `@SpringBootTest` makes the full application context available to 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.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());
    }

}
```

Place the test in `src/test/java`, in the same Java package as the view, so that the test can read the view’s package-private fields. 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 does.

## <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 browserless Vaadin session exists. If a test needs an application bean with `@VaadinSessionScope`, inject an `ObjectProvider` and resolve the bean after environment setup:

`CartViewSessionTest.java`

```java
import org.junit.jupiter.api.Assertions;
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.SpringBrowserlessTest;

@SpringBootTest
class CartViewSessionTest extends SpringBrowserlessTest {

    @Autowired
    private ObjectProvider<Cart> cartProvider;

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

        test(view.addButton).click();

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

This example assumes a session-scoped `Cart` service and a `CartView` whose **Add to cart** button, in the `addButton` field, adds an item to the cart. Resolve the bean in the test method, or in a subclass `@BeforeEach` method after the inherited environment setup. 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="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.

- [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`
