Spring Boot Test provides comprehensive testing support and utilities for Spring Boot applications
Utilities for web application testing including HtmlUnit integration, local test server information, and mock servlet context support.
org.springframework.boot.test.http.server - LocalTestWebServerorg.springframework.boot.test.web.htmlunit - HtmlUnit integrationorg.springframework.boot.test.mock.web - Mock servlet context// Local test server
import org.springframework.boot.test.http.server.LocalTestWebServer;
import org.springframework.boot.test.http.server.LocalTestWebServer.Scheme;
import org.springframework.boot.test.http.server.LocalTestWebServer.BaseUriDetails;
// HtmlUnit
import org.springframework.boot.test.web.htmlunit.UriBuilderFactoryWebClient;
import org.springframework.boot.test.web.htmlunit.UriBuilderFactoryWebConnectionHtmlUnitDriver;
// Mock servlet
import org.springframework.boot.test.mock.web.SpringBootMockServletContext;
// Supporting classes
import org.springframework.context.ApplicationContext;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriBuilderFactory;
import org.springframework.web.util.UriBuilder;
import org.htmlunit.html.HtmlPage;
import org.htmlunit.BrowserVersion;
import org.openqa.selenium.WebDriver;
import java.util.function.Supplier;Full Package: org.springframework.boot.test.http.server.LocalTestWebServer
Provides details of a locally running test web server.
public final class LocalTestWebServer {
// Get server information
public Scheme scheme(); // HTTP or HTTPS
public String uri(); // Base URI
public String uri(String uri); // URI with path
public UriBuilder uriBuilder(String uri); // URI builder
public UriBuilderFactory uriBuilderFactory(); // Factory for clients
// Path manipulation
public LocalTestWebServer withPath(String path); // New instance with path
// Factory methods
public static LocalTestWebServer of(Scheme scheme, int port);
public static LocalTestWebServer of(Scheme scheme, int port, String contextPath);
public static LocalTestWebServer of(Scheme scheme, Supplier<BaseUriDetails> detailsSupplier);
// Spring Boot integration
public static LocalTestWebServer obtain(ApplicationContext context); // Throws if not found
public static LocalTestWebServer get(ApplicationContext context); // Returns null if not found
public enum Scheme { HTTP, HTTPS }
/**
* Base URI details for a local test web server
* @param port the server port
* @param path the context path
* @since 4.0.0
*/
public record BaseUriDetails(int port, String path) {
/**
* Get the base URI for the given scheme
* @param scheme the scheme (HTTP or HTTPS)
* @return the base URI string (e.g., "http://localhost:8080/path")
*/
String uri(Scheme scheme);
/**
* Create new BaseUriDetails with appended path
* @param path the path to append
* @return new BaseUriDetails with updated path
*/
BaseUriDetails withPath(String path);
}
/**
* Internal strategy used to provide the running LocalTestWebServer
* Implementations can be registered in spring.factories and may accept an
* ApplicationContext constructor argument
* @since 4.0.0
*/
@FunctionalInterface
public interface Provider {
/**
* Return the provided LocalTestWebServer or null
* @return the local test web server or null if not available
*/
@Nullable
LocalTestWebServer getLocalTestWebServer();
}
}import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.http.server.LocalTestWebServer;
import org.springframework.context.ApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.junit.jupiter.api.Test;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ServerTest {
@Autowired
private ApplicationContext context;
@Test
void testServerInfo() {
LocalTestWebServer server = LocalTestWebServer.obtain(context);
String baseUri = server.uri(); // http://localhost:12345
String apiUri = server.uri("/api/users"); // http://localhost:12345/api/users
UriBuilderFactory factory = server.uriBuilderFactory();
// Use factory with RestClient, WebClient, etc.
}
}Full Package: org.springframework.boot.test.web.htmlunit.UriBuilderFactoryWebClient
HtmlUnit WebClient with automatic URI resolution.
public class UriBuilderFactoryWebClient extends WebClient {
public UriBuilderFactoryWebClient(UriBuilderFactory uriBuilderFactory);
// Overrides getPage to resolve URIs via factory
public <P extends Page> P getPage(String url) throws IOException, FailingHttpStatusCodeException;
}import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.boot.test.web.htmlunit.UriBuilderFactoryWebClient;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.htmlunit.html.HtmlPage;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class HtmlUnitTest {
@LocalServerPort
private int port;
@Test
void testWithWebClient() throws Exception {
DefaultUriBuilderFactory factory =
new DefaultUriBuilderFactory("http://localhost:" + port);
UriBuilderFactoryWebClient client = new UriBuilderFactoryWebClient(factory);
HtmlPage page = client.getPage("/");
assertThat(page.getTitleText()).contains("Home");
client.close();
}
}Full Package: org.springframework.boot.test.web.htmlunit.UriBuilderFactoryWebConnectionHtmlUnitDriver
Selenium WebDriver with automatic URI resolution.
public class UriBuilderFactoryWebConnectionHtmlUnitDriver
extends WebConnectionHtmlUnitDriver {
public UriBuilderFactoryWebConnectionHtmlUnitDriver(UriBuilderFactory factory);
public UriBuilderFactoryWebConnectionHtmlUnitDriver(UriBuilderFactory factory, boolean enableJavascript);
public UriBuilderFactoryWebConnectionHtmlUnitDriver(UriBuilderFactory factory, BrowserVersion version);
public UriBuilderFactoryWebConnectionHtmlUnitDriver(UriBuilderFactory factory, Capabilities capabilities);
@Override
public void get(String url); // Resolves URL via factory
}import org.springframework.boot.test.web.htmlunit.UriBuilderFactoryWebConnectionHtmlUnitDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class WebDriverTest {
@LocalServerPort
private int port;
@Test
void testWithWebDriver() {
DefaultUriBuilderFactory factory =
new DefaultUriBuilderFactory("http://localhost:" + port);
WebDriver driver = new UriBuilderFactoryWebConnectionHtmlUnitDriver(factory);
driver.get("/login");
driver.findElement(By.name("username")).sendKeys("user");
driver.findElement(By.name("password")).sendKeys("pass");
driver.findElement(By.tagName("form")).submit();
assertThat(driver.getCurrentUrl()).endsWith("/dashboard");
driver.quit();
}
}Full Package: org.springframework.boot.test.mock.web.SpringBootMockServletContext
Mock ServletContext that respects Spring Boot resource locations.
public class SpringBootMockServletContext extends MockServletContext {
public SpringBootMockServletContext(String resourceBasePath);
public SpringBootMockServletContext(String resourceBasePath, ResourceLoader resourceLoader);
// Searches these locations:
// - classpath:META-INF/resources
// - classpath:resources
// - classpath:static
// - classpath:public
}import org.springframework.boot.test.mock.web.SpringBootMockServletContext;
import java.net.URL;
class MockServletTest {
@Test
void testResourceLoading() throws Exception {
SpringBootMockServletContext context =
new SpringBootMockServletContext("");
URL resource = context.getResource("/static/css/app.css");
assertThat(resource).isNotNull();
}
}Issue: LocalTestWebServer.obtain() throws exception
Issue: HtmlUnit NullPointerException
Issue: WebDriver elements not found
Issue: Resources not found in mock servlet context
Install with Tessl CLI
npx tessl i tessl/maven-org-springframework-boot--spring-boot-test