Annotation Interface TestBean
@TestBean is the Juneau analog of Spring's @MockBean / @TestConfiguration, scoped to a
BeanStore overlay rather than a Spring application context. The JuneauBeanStoreExtension discovers
@TestBean-annotated members on the test class, builds a TestBeanStore overlay containing the
declared overrides, and exposes it via JuneauBeanStoreExtension.getStore() so the test author can wire
it into the SUT explicitly (typically via a builder hook such as
MockRestClient.Builder.overridingBeanStore(...)).
The wiring is intentionally explicit: the extension does not auto-discover RestContext /
Microservice fields on the test class. Test authors write one line of plumbing
(.overridingBeanStore(ext.getStore())) and get full control.
The annotation supports two wiring modes via mode(): Mode INJECT (default, fresh-instance with
overrides via the SUT's builder) and Mode OVERLAY (existing-instance push/pop against a long-lived
SUT's bean store). See JuneauBeanStoreExtension for the full Mode INJECT / Mode OVERLAY contracts.
Applies to:
- Fields — the field's current value (read via reflection) is registered as the override.
- Methods — a parameterless factory method whose return type is type-compatible with the
override target. Invoked once per scope (per-method for
Scope.METHOD, per-class forScope.CLASS).
Example:
See Also:
JuneauBeanStoreExtension- The JUnit 5 extension that discovers and applies these annotations.TestBeanStore- The overlay builder the extension populates.Scope- Per-method vs per-class lifecycle selector.
- Since:
- 10.0.0
-
Optional Element Summary
Optional Elements
-
Element Details
-
name
Qualifier name for the override.Empty string (the default) means the override is registered as the unnamed bean of its type, matching the behavior of
TestBeanStore.override(Class, Object)without a name argument.When non-empty, the override is registered under this qualifier so the framework's
@Bean(name = "...")parameter resolution can find it. Two@TestBean-declared overrides of the same type but different names are both picked up — the qualifier is the disambiguator.- Returns:
- The qualifier name. Empty string means "unnamed".
- Since:
- 10.0.0
- Default:
- ""
-
type
Explicit override target type.Object.class(the default) is a sentinel meaning "use the annotated member's declared type" — the field's declared type for field overrides, the method's return type for method overrides. This is the common case.Set this explicitly when the member's declared type is a supertype of the intended registration type, for example an
Objectfield carrying aMyServicevalue, or a@TestBean Supplier<MyService>factory method whose semantic type isMyService.- Returns:
- The override target type.
Object.classmeans "use the member's declared type". - Since:
- 10.0.0
- Default:
- java.lang.Object.class
-
scope
Lifecycle scope of the override.Defaults to
Scope.METHOD— the override is rebuilt for every test method. UseScope.CLASSto share the override across all test methods in the class.CLASS-scope overrides may only be declared onstaticfields andstaticmethods. Instance members are not visible atbeforeAlltime and will be rejected with an error.- Returns:
- The lifecycle scope.
- Since:
- 10.0.0
- Default:
- METHOD
-
mode
Wiring mode for the override.Defaults to
Mode.INJECT— the override is installed at SUT construction time via the SUT'soverridingBeanStore(...)builder hook. UseMode.OVERLAYfor push/pop overlays against a long-lived SUT that the test has attach-ed to the extension.Mixing
Mode.INJECTandMode.OVERLAYannotations on the same scope is rejected at scope-build time with a clearIllegalStateException.- Returns:
- The wiring mode.
- Since:
- 10.0.0
- Default:
- INJECT
-