or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

assertj-integration.mdcontext-runners.mdindex.mdintegration-testing.mdjson-testing.mdoutput-capture.mdtest-configuration.mdtest-properties.mdweb-test-utilities.md
tile.json

web-test-utilities.mddocs/

Web Test Utilities

Utilities for web application testing including HtmlUnit integration, local test server information, and mock servlet context support.

Packages

  • org.springframework.boot.test.http.server - LocalTestWebServer
  • org.springframework.boot.test.web.htmlunit - HtmlUnit integration
  • org.springframework.boot.test.mock.web - Mock servlet context

Prerequisites

  • HtmlUnit dependencies for web client/driver
  • Servlet API for mock servlet context
  • @SpringBootTest with web environment for LocalTestWebServer

Core Imports

// 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;

LocalTestWebServer

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

Pattern: Obtain from Context

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.
    }
}

HtmlUnit Integration

UriBuilderFactoryWebClient

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

Pattern: HtmlUnit Testing

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

UriBuilderFactoryWebConnectionHtmlUnitDriver

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
}

Pattern: WebDriver Testing

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

SpringBootMockServletContext

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
}

Pattern: Mock Servlet Context

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

Troubleshooting

Issue: LocalTestWebServer.obtain() throws exception

  • Cause: Not using RANDOM_PORT or DEFINED_PORT
  • Solution: Set webEnvironment = WebEnvironment.RANDOM_PORT

Issue: HtmlUnit NullPointerException

  • Cause: UriBuilderFactory is null
  • Solution: Create factory with base URI before web client

Issue: WebDriver elements not found

  • Cause: JavaScript not enabled
  • Solution: Use constructor with enableJavascript=true

Issue: Resources not found in mock servlet context

  • Cause: Wrong resource location
  • Solution: Use standard Spring Boot locations (static/, public/, etc.)

See Also

  • Integration Testing - @SpringBootTest with web environment
  • Context Runners - WebApplicationContextRunner