Package io.microsphere.spring.test.util
Class SpringTestUtils
- java.lang.Object
-
- io.microsphere.spring.test.util.SpringTestUtils
-
public abstract class SpringTestUtils extends java.lang.ObjectSpring Test Utilities class- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
ConfigurableApplicationContext
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidtestInSpringContainer(io.microsphere.lang.function.ThrowableBiConsumer<org.springframework.context.ConfigurableApplicationContext,org.springframework.core.env.ConfigurableEnvironment> consumer, java.lang.Class<?>... configClasses)Executes the givenThrowableBiConsumerwithin a Spring container context.static voidtestInSpringContainer(io.microsphere.lang.function.ThrowableConsumer<org.springframework.context.ConfigurableApplicationContext> consumer, java.lang.Class<?>... configClasses)Executes the givenThrowableConsumerwithin a Spring container context.
-
-
-
Method Detail
-
testInSpringContainer
public static void testInSpringContainer(io.microsphere.lang.function.ThrowableConsumer<org.springframework.context.ConfigurableApplicationContext> consumer, java.lang.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:
java.lang.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, java.lang.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:
java.lang.NullPointerException- if the given consumer is null- See Also:
ThrowableBiConsumer,AnnotationConfigApplicationContext
-
-