Class TestBeanScope

java.lang.Object
io.avaje.inject.test.TestBeanScope

@NullMarked public abstract class TestBeanScope extends Object
Provides access to the global "test scope" and helper methods to use it.
  • Constructor Details

    • TestBeanScope

      public TestBeanScope()
  • Method Details

    • builder

      public static BeanScopeBuilder builder()
      Build and return a new BeanScope that will have the global "test scope" as its parent.

      That is, beans created using @TestScope are all in the global "test scope" which is a parent to this scope.

      
      
        try (BeanScope testScope = TestBeanScope.builder()
         .forTesting()
         .mock(MyDataApi.class)
         .build()) {
      
         // mockito mock, use when() etc as needed
         var myDataApi = testScope.get(MyDataApi.class);
      
         var myService = testScope.get(MyService.class);
      
         ...
        }
      
       
      Returns:
      A new test BeanScope with the global "test scope" as its parent.
    • initialise

      public static @Nullable BeanScope initialise()
      Return the BeanScope for @TestScope beans ONLY building once.

      If the BeanScope has already been created then that shared scope instance is returned. This will be the same BeanScope as is used by @InjectTest.

      The global test scope is closed when either JUnit closes or on JVM shutdown.

      
      
         final BeanScope globalTestScope = TestBeanScope.initialise();
      
       
      Returns:
      The test scope BeanScope (nullable).
    • create

      public static @Nullable BeanScope create(boolean shutdownHook)
      Build and return a new BeanScope for @TestScope beans. This builds the beans in the test scope every time it is called.

      Generally, we DO NOT want to use this method but use initialise() instead.

      Parameters:
      shutdownHook - Set whether a shutdown hook should be created to close the BeanScope.
      Returns:
      The test scope BeanScope (nullable).