Class SpringTestUtils


  • public abstract class SpringTestUtils
    extends java.lang.Object
    Spring 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 void testInSpringContainer​(io.microsphere.lang.function.ThrowableBiConsumer<org.springframework.context.ConfigurableApplicationContext,​org.springframework.core.env.ConfigurableEnvironment> consumer, java.lang.Class<?>... configClasses)
      Executes the given ThrowableBiConsumer within a Spring container context.
      static void testInSpringContainer​(io.microsphere.lang.function.ThrowableConsumer<org.springframework.context.ConfigurableApplicationContext> consumer, java.lang.Class<?>... configClasses)
      Executes the given ThrowableConsumer within a Spring container context.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • testInSpringContainer

        public static void testInSpringContainer​(io.microsphere.lang.function.ThrowableConsumer<org.springframework.context.ConfigurableApplicationContext> consumer,
                                                 java.lang.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 {@link RuntimeException}.
         
        Parameters:
        consumer - the instance of ThrowableConsumer to execute
        configClasses - 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 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 {@link RuntimeException}.
         
        Parameters:
        consumer - the instance of ThrowableBiConsumer to execute
        configClasses - 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