BeanOverride<T extends Object> class final

Replaces one of the application's beans with an object of the test, a fake or a mock, in a context started by springBootTest or ApplicationContextRunner.

Use it when the real bean talks to something a test cannot use: a payment provider, the wall clock, a mail server. The original definition is removed before any bean is created, so the real object (and whatever it would connect to) is never built, and every bean that injects it gets instance. The replacement keeps the original's name, aliases, qualifiers, order and @Primary / @Fallback flags, so @Qualifier injection points still find it. It is registered under T only (use BeanOverride.named to keep the original's types, or to give the fake's).

Which bean: the one called name (or whose alias it is) when given; else the only bean of T; among several, the single @Primary one, and a StateError listing them when there is no single primary. Without any bean of T, instance is simply registered as a new bean. Any mock library works (for example mocktail); reset shared mocks in setUp, since nothing is reset between tests.

final gateway = FakeGateway();
final app = springBootTest(
  springApplication,
  overrides: [
    // No real payment: the checkout charges the fake.
    BeanOverride<PaymentGateway>(gateway),
    // One of several mailers, chosen by name.
    BeanOverride<Mailer>(MockMailer(), name: 'mailer'),
  ],
);

Spring (Java): @MockitoBean and @TestBean. Here the object is your own, ready-made; no mock is generated.

@MockitoBean PaymentGateway gateway;

Constructors

BeanOverride(T instance, {String? name})
Replaces the bean of type T, or the bean called name, with instance.
const
BeanOverride.named(T instance, {required String name, List<Type>? types})
Replaces the bean called name with instance, whatever the bean's type; this is what a @TestBean(name: ...) field generates.
const

Properties

hashCode → int
The hash code for this object.
no setterinherited
instance → T
The object that every injection point of the replaced bean receives.
final
name → String?
The name, or an alias, of the bean to replace; when given it wins over the choice by type. Needed when several beans of T exist and none is @Primary.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
type → Type
The type the replacement is registered under, T.
no setter
types → List<Type>?
The types a BeanOverride.named registers instance under, besides T and its runtime class; null keeps the replaced bean's (see there).
final

Methods

apply(ConfigurableListableBeanFactory factory, BeanDefinitionRegistry registry) → void
Removes the chosen bean definition from registry and registers instance in its place; factory is used to find the beans of T.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
override

Operators

operator ==(Object other) → bool
The equality operator.
inherited