Skip to content
Latchkey

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.

junit
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' available

Common 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.

java
@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

  1. Provide every @Value/@ConfigurationProperties key in the test profile.
  2. Ensure the bean's package is under @SpringBootApplication/@ComponentScan.
  3. Follow the deepest Caused by to 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.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →