Spring "BeanCreationException" in CI - Fix the Failing Bean
BeanCreationException is thrown when Spring cannot instantiate or wire a bean during context startup. The message names the bean; the nested cause says why - an unsatisfied dependency, a missing property, or an exception in the bean's constructor/@PostConstruct.
What this error means
Context startup (often inside a Spring test) fails with Error creating bean with name 'x' and a chain: UnsatisfiedDependencyException -> NoSuchBeanDefinitionException, or a constructor exception. The same bean fails on every run.
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'userController' defined in ...:
Unsatisfied dependency expressed through constructor parameter 0
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'com.example.UserService' availableCommon causes
Unsatisfied or ambiguous dependency
A required bean is not defined (missing @Component/@Bean, not component-scanned) or two candidates exist with no @Primary/@Qualifier, so wiring fails.
The bean threw during construction
A constructor, @Value placeholder, or @PostConstruct that fails (missing property, bad config) aborts bean creation.
How to fix it
Define or qualify the missing bean
Make the dependency a managed bean, or disambiguate when several exist.
@Service // make it scannable
public class UserService { ... }
@Configuration
class AppConfig {
@Bean @Primary // pick a default when several candidates exist
Clock clock() { return Clock.systemUTC(); }
}Resolve placeholders and scan packages
- Provide every
@Value/@ConfigurationPropertieskey in the test profile. - Ensure the bean's package is under
@SpringBootApplication/@ComponentScan. - Follow the deepest
Caused byto the actual constructor/config failure and fix that.
How to prevent it
- Component-scan the right packages, qualify ambiguous beans, and supply all required properties in the test profile so every bean can be created.