Class TestBeanStore

java.lang.Object
org.apache.juneau.commons.inject.BasicBeanStore
org.apache.juneau.bct.junit.TestBeanStore
All Implemented Interfaces:
AutoCloseable, BeanStore, WritableBeanStore

public class TestBeanStore extends BasicBeanStore
Fluent BasicBeanStore subclass for declaring test-time bean overrides.

A TestBeanStore is an overlay-builder. Tests register replacements for production beans via override(java.lang.Class<T>, T) and then plug the resulting store into the SUT — typically through a builder hook such as MockRestClient.Builder.overridingBeanStore(...) (the Mode INJECT wiring pattern; see JuneauBeanStoreExtension for the full contract). The framework threads the overlay into the overridingParent slot of the SUT's bean store, putting registered overrides at tier 1 of the resolution chain (above local resource entries and the Spring bridge).

No JUnit 5 dependency. This class is intentionally a plain BasicBeanStore subclass so that callers from non-JUnit harnesses (TestNG, raw main(...), Spring Test, etc.) can use it without dragging in junit-jupiter-api.

Example:

// Build the overlay. var overlay = new TestBeanStore() .override(MyExternalApi.class, mockApi) .override(RichLogger.class, () -> spyLogger); // Wire into the SUT. try (client = MockRestClient.create(MyResource.class) .overridingBeanStore(overlay) .build()) { client.get("/widgets/1").run().assertStatus().is(200); }

See Also:
  • BasicBeanStore - The bean store this overlay extends.
  • StackOverlay - The stack-aware composition primitive used for class-scope and Mode OVERLAY layering.
  • JuneauBeanStoreExtension - Mode INJECT vs Mode OVERLAY contracts and the JUnit 5 lifecycle owner.
Since:
10.0.0
  • Constructor Details

    • TestBeanStore

      public TestBeanStore()
      Constructor.
    • TestBeanStore

      public TestBeanStore(BeanStore parent)
      Parent-chaining constructor.

      Lookups consult this store's local entries first and then fall through to the supplied parent. Used by JuneauBeanStoreExtension to stack a per-method overlay on top of a per-class overlay so method-scope overrides shadow class-scope overrides of the same (type, name) pair.

      Parameters:
      parent - The parent bean store. Can be null for a standalone overlay.
      Since:
      10.0.0
  • Method Details

    • override

      public <T> TestBeanStore override(Class<T> type, T bean)
      Registers an unnamed bean as a test-time override.
      Type Parameters:
      T - The bean type.
      Parameters:
      type - The bean type. Must not be null.
      bean - The override instance. Can be null (stored as an explicit null binding, distinct from no binding at all).
      Returns:
      This object.
    • override

      public <T> TestBeanStore override(Class<T> type, T bean, String name)
      Registers a named bean as a test-time override.
      Type Parameters:
      T - The bean type.
      Parameters:
      type - The bean type. Must not be null.
      bean - The override instance. Can be null (stored as an explicit null binding, distinct from no binding at all).
      name - The bean name. Can be null for unnamed beans.
      Returns:
      This object.
    • override

      public <T> TestBeanStore override(Class<T> type, Supplier<T> supplier)
      Registers an unnamed supplier-backed bean as a test-time override.

      The supplier is invoked lazily on first lookup, mirroring BasicBeanStore.addSupplier(java.lang.Class<T>, java.util.function.Supplier<T>).

      Type Parameters:
      T - The bean type.
      Parameters:
      type - The bean type. Must not be null.
      supplier - The override supplier. Must not be null.
      Returns:
      This object.
    • override

      public <T> TestBeanStore override(Class<T> type, Supplier<T> supplier, String name)
      Registers a named supplier-backed bean as a test-time override.

      The supplier is invoked lazily on first lookup, mirroring BasicBeanStore.addSupplier(java.lang.Class<T>, java.util.function.Supplier<T>).

      Type Parameters:
      T - The bean type.
      Parameters:
      type - The bean type. Must not be null.
      supplier - The override supplier. Must not be null.
      name - The bean name. Can be null for unnamed beans.
      Returns:
      This object.