Package io.microsphere.spring.test.util
Class SpringTestUtils
java.lang.Object
io.microsphere.spring.test.util.SpringTestUtils
Spring Test Utilities class
- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
-
ConfigurableApplicationContext
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidtestInSpringContainer(io.microsphere.lang.function.ThrowableBiConsumer<org.springframework.context.ConfigurableApplicationContext, org.springframework.core.env.ConfigurableEnvironment> consumer, Class<?>... configClasses) Executes the givenThrowableBiConsumerwithin a Spring container context.static voidtestInSpringContainer(io.microsphere.lang.function.ThrowableConsumer<org.springframework.context.ConfigurableApplicationContext> consumer, Class<?>... configClasses) Executes the givenThrowableConsumerwithin a Spring container context.
-
Method Details
-
testInSpringContainer
public static void testInSpringContainer(io.microsphere.lang.function.ThrowableConsumer<org.springframework.context.ConfigurableApplicationContext> consumer, Class<?>... configClasses) Executes the givenThrowableConsumerwithin a Spring container context.This method creates a new
AnnotationConfigApplicationContextusing 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 {@link RuntimeException}.- Parameters:
consumer- the instance ofThrowableConsumerto executeconfigClasses- one or more configuration classes to be registered in the Spring context- Throws:
NullPointerException- if the given consumer is null
-
testInSpringContainer
public static void testInSpringContainer(io.microsphere.lang.function.ThrowableBiConsumer<org.springframework.context.ConfigurableApplicationContext, org.springframework.core.env.ConfigurableEnvironment> consumer, Class<?>... configClasses) Executes the givenThrowableBiConsumerwithin a Spring container context.This method creates a new
AnnotationConfigApplicationContextusing 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 {@link RuntimeException}.- Parameters:
consumer- the instance ofThrowableBiConsumerto executeconfigClasses- one or more configuration classes to be registered in the Spring context- Throws:
NullPointerException- if the given consumer is null- See Also:
-
ThrowableBiConsumerAnnotationConfigApplicationContext
-