Bird
0
0

A test using @DataJpaTest fails with an error: "No qualifying bean of type 'UserRepository' available." What is the most probable fix?

medium📝 Debug Q7 of 15
Spring Boot - Testing Spring Boot Applications
A test using @DataJpaTest fails with an error: "No qualifying bean of type 'UserRepository' available." What is the most probable fix?
AAdd <code>@EntityScan</code> to the test class
BAdd <code>@Repository</code> annotation to the UserRepository interface
CAdd <code>@EnableJpaRepositories</code> to the test class
DAdd <code>@ComponentScan</code> to the test class
Step-by-Step Solution
Solution:
  1. Step 1: Understand the bean availability error

    @DataJpaTest scans for JPA repositories only in the test class's package and subpackages. If UserRepository is elsewhere, no bean is available.
  2. Step 2: Identify the fix

    Adding @EnableJpaRepositories to the test class (with basePackages if needed) enables repository detection.
  3. Final Answer:

    Add @EnableJpaRepositories to the test class -> Option C
  4. Quick Check:

    No repo bean = add @EnableJpaRepositories [OK]
Quick Trick: Add @EnableJpaRepositories for repo scanning in @DataJpaTest [OK]
Common Mistakes:
  • Adding @Repository to Spring Data interfaces (not required)
  • Using @EntityScan (for entities only)
  • Adding @ComponentScan unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes