Skip to content

io microsphere spring test util SpringTestWebUtils

github-actions[bot] edited this page Jun 18, 2026 · 18 revisions

SpringTestWebUtils

Type: Class | Module: microsphere-spring-test | Package: io.microsphere.spring.test.util | Since: 1.0.0

Source: microsphere-spring-test/src/main/java/io/microsphere/spring/test/util/SpringTestWebUtils.java

Overview

Web Utilities class for Spring Test

Declaration

public abstract class SpringTestWebUtils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.2.27-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

createWebRequest

NativeWebRequest request = SpringTestWebUtils.createWebRequest();

createWebRequest

NativeWebRequest request = SpringTestWebUtils.createWebRequest(req -> {
    req.setMethod("POST");
    req.setRequestURI("/api/users");
    req.addHeader("Content-Type", "application/json");
    req.setParameter("id", "123");
});

createWebRequest

NativeWebRequest request = SpringTestWebUtils.createWebRequest("/api/users/123");

createWebRequestWithParams

NativeWebRequest request = SpringTestWebUtils.createWebRequestWithParams(
    "name", "John Doe",
    "age", "30"
);

createWebRequestWithHeaders

NativeWebRequest request = SpringTestWebUtils.createWebRequestWithHeaders(
    "Content-Type", "application/json",
    "Authorization", "Bearer token123"
);

createWebRequestWithHeaders

Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer token123");
NativeWebRequest request = SpringTestWebUtils.createWebRequestWithHeaders(headers);

createPreFightRequest

NativeWebRequest preFlightRequest = SpringTestWebUtils.createPreFightRequest();

clearAttributes

NativeWebRequest request = SpringTestWebUtils.createWebRequest();
request.setAttribute("key1", "value1", RequestAttributes.SCOPE_REQUEST);
request.setAttribute("key2", "value2", RequestAttributes.SCOPE_REQUEST);

// Clear all request-scoped attributes
SpringTestWebUtils.clearAttributes(request);

clearAttributes

NativeWebRequest request = SpringTestWebUtils.createWebRequest();
request.setAttribute("key1", "value1", RequestAttributes.SCOPE_REQUEST);
request.setAttribute("key2", "value2", RequestAttributes.SCOPE_SESSION);

// Clear all request-scoped attributes
SpringTestWebUtils.clearAttributes(request, RequestAttributes.SCOPE_REQUEST);

// Clear all session-scoped attributes
SpringTestWebUtils.clearAttributes(request, RequestAttributes.SCOPE_SESSION);

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-spring-test</artifactId>
    <version>${microsphere-spring.version}</version>
</dependency>

Tip: Use the BOM (microsphere-spring-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.spring.test.util.SpringTestWebUtils;

API Reference

Public Methods

Method Description
createWebRequest Creates a new instance of NativeWebRequest with default settings.
createWebRequest Creates a new instance of NativeWebRequest with custom settings provided by the given Consumer.
createWebRequest Creates a new instance of NativeWebRequest with the specified request URI.
createWebRequestWithParams Creates a new instance of NativeWebRequest with the specified request parameters.
createWebRequestWithHeaders Creates a new instance of NativeWebRequest with the specified request headers.
createWebRequestWithHeaders Creates a new instance of NativeWebRequest with the specified request headers.
createPreFightRequest Creates a new instance of NativeWebRequest for CORS pre-flight requests.
clearAttributes Clears all attributes from the request scope of the given NativeWebRequest.
clearAttributes Clears all attributes from the specified scope of the given NativeWebRequest.

Method Details

createWebRequest

public static NativeWebRequest createWebRequest()

Creates a new instance of NativeWebRequest with default settings.

This method is equivalent to calling #createWebRequest(Consumer) with an empty consumer.

Example Usage

`NativeWebRequest request = SpringTestWebUtils.createWebRequest();
`

createWebRequest

public static NativeWebRequest createWebRequest(Consumer<MockHttpServletRequest> requestBuilder)

Creates a new instance of NativeWebRequest with custom settings provided by the given Consumer.

This method allows for flexible configuration of the underlying MockHttpServletRequest before creating the ServletWebRequest. The consumer can modify any aspect of the request, such as headers, parameters, method, URI, etc.

Example Usage

`NativeWebRequest request = SpringTestWebUtils.createWebRequest(req -> {
    req.setMethod("POST");
    req.setRequestURI("/api/users");
    req.addHeader("Content-Type", "application/json");
    req.setParameter("id", "123");
`);
}

createWebRequest

public static NativeWebRequest createWebRequest(String requestURI)

Creates a new instance of NativeWebRequest with the specified request URI.

This method configures the underlying MockHttpServletRequest with the given URI and sets the #PATH_ATTRIBUTE to the same value. It's a convenient way to create a web request for a specific endpoint.

Example Usage

`NativeWebRequest request = SpringTestWebUtils.createWebRequest("/api/users/123");
`

createWebRequestWithParams

public static NativeWebRequest createWebRequestWithParams(Object... params)

Creates a new instance of NativeWebRequest with the specified request parameters.

This method configures the underlying MockHttpServletRequest with the given parameters using io.microsphere.collection.MapUtils#of(Object...). The parameters are passed as key-value pairs in the form of key1, value1, key2, value2, ....

Example Usage

`NativeWebRequest request = SpringTestWebUtils.createWebRequestWithParams(
    "name", "John Doe",
    "age", "30"
);
`

createWebRequestWithHeaders

public static NativeWebRequest createWebRequestWithHeaders(Object... headers)

Creates a new instance of NativeWebRequest with the specified request headers.

This method configures the underlying MockHttpServletRequest with the given headers using io.microsphere.collection.MapUtils#of(Object...). The headers are passed as key-value pairs in the form of key1, value1, key2, value2, ....

Example Usage

`NativeWebRequest request = SpringTestWebUtils.createWebRequestWithHeaders(
    "Content-Type", "application/json",
    "Authorization", "Bearer token123"
);
`

createWebRequestWithHeaders

public static NativeWebRequest createWebRequestWithHeaders(Map<String, String> headers)

Creates a new instance of NativeWebRequest with the specified request headers.

This method configures the underlying MockHttpServletRequest with the given headers using a Map. The headers are passed as a map of key-value pairs.

Example Usage

`Map headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer token123");
NativeWebRequest request = SpringTestWebUtils.createWebRequestWithHeaders(headers);
`

createPreFightRequest

public static NativeWebRequest createPreFightRequest()

Creates a new instance of NativeWebRequest for CORS pre-flight requests.

This method configures the underlying MockHttpServletRequest with the OPTIONS method and adds the following headers:

  • :METHOD: - set to "OPTIONS"
  • Origin - set to "*"
  • Access-Control-Request-Method - set to "*"

Example Usage

`NativeWebRequest preFlightRequest = SpringTestWebUtils.createPreFightRequest();
`

clearAttributes

public static void clearAttributes(@Nonnull NativeWebRequest request)

Clears all attributes from the request scope of the given NativeWebRequest.

This method removes all attributes currently set in the request scope. It's useful for cleaning up request state between tests or operations.

Example Usage

`NativeWebRequest request = SpringTestWebUtils.createWebRequest();
request.setAttribute("key1", "value1", RequestAttributes.SCOPE_REQUEST);
request.setAttribute("key2", "value2", RequestAttributes.SCOPE_REQUEST);

// Clear all request-scoped attributes
SpringTestWebUtils.clearAttributes(request);
`

clearAttributes

public static void clearAttributes(@Nonnull NativeWebRequest request, int scope)

Clears all attributes from the specified scope of the given NativeWebRequest.

This method removes all attributes currently set in the specified scope (either request or session). It's useful for cleaning up request or session state between tests or operations.

Example Usage

`NativeWebRequest request = SpringTestWebUtils.createWebRequest();
request.setAttribute("key1", "value1", RequestAttributes.SCOPE_REQUEST);
request.setAttribute("key2", "value2", RequestAttributes.SCOPE_SESSION);

// Clear all request-scoped attributes
SpringTestWebUtils.clearAttributes(request, RequestAttributes.SCOPE_REQUEST);

// Clear all session-scoped attributes
SpringTestWebUtils.clearAttributes(request, RequestAttributes.SCOPE_SESSION);
`

See Also

  • NativeWebRequest
  • MockHttpServletRequest

This documentation was auto-generated from the source code of microsphere-spring.

Home

spring-context

spring-guice

spring-jdbc

spring-test

spring-web

spring-webflux

spring-webmvc

Clone this wiki locally