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.
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:
Source code
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>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:
Source code
HelloWorldView.java
HelloWorldView.javaimport 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:
Source code
HelloWorldViewTest.java
HelloWorldViewTest.javaimport 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 for what each line of the test does.
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.
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:
Source code
CartViewSessionTest.java
CartViewSessionTest.javaimport 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 for scope guarantees and other deferred-lookup options.
Next Steps
-
Test User Interactions explains how the test works and how to cover more complex views.
-
Debug a Failing Browserless Test helps when a test fails.
-
Test Environment and Lifecycle describes the environment lifecycle and the navigation API.
7F423DA0-1C41-44BA-B832-55C269FA9311