Skip to content
Latchkey

Spring Boot "bean ... could not be registered ... overriding is disabled" in CI

Since Spring Boot 2.1 bean overriding is off by default. When two definitions share a name, the container throws a BeanDefinitionOverrideException instead of silently replacing one. This often surfaces in tests that add a duplicate bean.

What this error means

Startup or a test fails with "The bean 'X', defined in ..., could not be registered. A bean with that name has already been defined ... and overriding is disabled".

Spring Boot
org.springframework.beans.factory.support.BeanDefinitionOverrideException:
Invalid bean definition with name 'clock' defined in ...: Cannot register bean
definition ... there is already ... overriding is disabled.

Common causes

A test config redefines an existing bean

A @TestConfiguration or @Configuration declares a bean whose name already exists in the main context.

Two configurations declare the same bean name

Two @Bean methods (or a component and a config) produce the same name, which used to be silently overridden.

How to fix it

Prefer replacing the bean cleanly

  1. In tests, use @MockBean/@SpyBean, which replace the existing bean without a name clash.
  2. Or give the test bean a distinct name and inject by qualifier.
  3. Re-run so no duplicate definition exists.
src/test/java
@MockBean Clock clock;

Allow overriding only where intended

If an intentional override is needed, enable it explicitly (scope it to tests).

application-test.properties
spring.main.allow-bean-definition-overriding=true

How to prevent it

  • Use @MockBean/@SpyBean in tests instead of redefining beans.
  • Keep bean names unique across configurations.
  • Enable overriding narrowly (test profile) rather than globally.

Frequently asked questions

What causes ""bean definition ... overriding is disabled""?
A @TestConfiguration or @Configuration declares a bean whose name already exists in the main context.
How do I fix "bean definition ... overriding is disabled"?
Prefer replacing the bean cleanly

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card