Skip to content
Latchkey

Spring Boot "NoSuchBeanDefinitionException" in CI

Spring looked up a bean by type or name and the container has none registered. Either the class is not component-scanned, its @Configuration is excluded in this profile, or a test slice does not load it.

What this error means

Startup or a test fails with "No qualifying bean of type 'X' available: expected at least 1 bean which qualifies as autowire candidate".

Spring Boot
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of
type 'com.example.PaymentClient' available: expected at least 1 bean which qualifies
as autowire candidate.

Common causes

The component is outside the scanned packages

The bean lives in a package the @SpringBootApplication base package does not cover, so component scanning never registers it.

A test slice loads only part of the context

Slices like @WebMvcTest or @DataJpaTest load a subset of beans; a service outside that slice is absent unless mocked or imported.

How to fix it

Bring the bean into scope

  1. Confirm the class is annotated (@Component/@Service) and under the scanned base package.
  2. In a slice test, add @Import for the config, or replace the collaborator with @MockBean.
  3. Re-run to confirm the container now finds the bean.
src/test/java
@WebMvcTest(OrderController.class)
@Import(OrderService.class)
class OrderControllerTest { /* ... */ }

Mock the missing collaborator in slice tests

When a slice intentionally excludes a bean, provide a mock so the wiring is satisfied.

src/test/java
@MockBean PaymentClient paymentClient;

How to prevent it

  • Keep components under the application base package or add explicit scan config.
  • For slice tests, import only what the slice needs or mock the rest.
  • Do not rely on a full context when a slice would be faster and clearer.

Frequently asked questions

What causes ""NoSuchBeanDefinitionException""?
The bean lives in a package the @SpringBootApplication base package does not cover, so component scanning never registers it.
How do I fix "NoSuchBeanDefinitionException"?
Bring the bean into scope

Related guides

References

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