Skip to content

io microsphere spring test util SpringTestUtils

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

SpringTestUtils

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/SpringTestUtils.java

Overview

Spring Test Utilities class

Declaration

public abstract class SpringTestUtils

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

testInSpringContainer

// Example 1: Basic usage without any exception
SpringTestContext.testInSpringContainer(context -> {
    MyService myService = context.getBean(MyService.class);
    assertNotNull(myService);
}, MyConfig.class);

// Example 2: Usage where the consumer throws an exception
SpringTestContext.testInSpringContainer(context -> {
    throw new RuntimeException("Test Exception");
}, MyConfig.class);
// The thrown exception will be propagated as a {@link RuntimeException}.

testInSpringContainer

// Example 1: Basic usage without any exception
SpringTestContext.testInSpringContainer((context, environment) -> {
    MyService myService = context.getBean(MyService.class);
    assertNotNull(myService);
}, MyConfig.class);

// Example 2: Usage where the consumer throws an exception
SpringTestContext.testInSpringContainer((context, environment) -> {
    throw new RuntimeException("Test Exception");
}, MyConfig.class);
// The thrown exception will be propagated as a {@link RuntimeException}.

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.SpringTestUtils;

API Reference

Public Methods

Method Description
testInSpringContainer Executes the given ThrowableConsumer within a Spring container context.
testInSpringContainer Executes the given ThrowableBiConsumer within a Spring container context.

Method Details

testInSpringContainer

public static void testInSpringContainer(ThrowableConsumer<ConfigurableApplicationContext> consumer, Class<?>... configClasses)

Executes the given ThrowableConsumer within a Spring container context.

This method creates a new AnnotationConfigApplicationContext using the provided configuration classes and passes the context to the consumer for execution.

Example Usage

`// Example 1: Basic usage without any exception
SpringTestContext.testInSpringContainer(context -> {
    MyService myService = context.getBean(MyService.class);
    assertNotNull(myService);
`, MyConfig.class);

// Example 2: Usage where the consumer throws an exception
SpringTestContext.testInSpringContainer(context -> {
    throw new RuntimeException("Test Exception");
}, MyConfig.class);
// The thrown exception will be propagated as a `RuntimeException`.
}

testInSpringContainer

public static void testInSpringContainer(ThrowableBiConsumer<ConfigurableApplicationContext, ConfigurableEnvironment> consumer, Class<?>... configClasses)

Executes the given ThrowableBiConsumer within a Spring container context.

This method creates a new AnnotationConfigApplicationContext using the provided configuration classes and passes both the context and its environment to the consumer for execution.

Example Usage

`// Example 1: Basic usage without any exception
SpringTestContext.testInSpringContainer((context, environment) -> {
    MyService myService = context.getBean(MyService.class);
    assertNotNull(myService);
`, MyConfig.class);

// Example 2: Usage where the consumer throws an exception
SpringTestContext.testInSpringContainer((context, environment) -> {
    throw new RuntimeException("Test Exception");
}, MyConfig.class);
// The thrown exception will be propagated as a `RuntimeException`.
}

See Also

  • ConfigurableApplicationContext

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