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".
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
- Confirm the class is annotated (
@Component/@Service) and under the scanned base package. - In a slice test, add
@Importfor the config, or replace the collaborator with@MockBean. - Re-run to confirm the container now finds the bean.
@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.
@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.