Class TestBeanScope
java.lang.Object
io.avaje.inject.test.TestBeanScope
Provides access to the global "test scope" and helper methods to use it.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic BeanScopeBuilderbuilder()Build and return a new BeanScope that will have the global "test scope" as its parent.static @Nullable BeanScopecreate(boolean shutdownHook) Build and return a new BeanScope for@TestScopebeans.static @Nullable BeanScopeReturn the BeanScope for@TestScopebeans ONLY building once.
-
Constructor Details
-
TestBeanScope
public TestBeanScope()
-
-
Method Details
-
builder
Build and return a new BeanScope that will have the global "test scope" as its parent.That is, beans created using
@TestScopeare 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
Return the BeanScope for@TestScopebeans 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
Build and return a new BeanScope for@TestScopebeans. 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).
-