Bird
0
0

You wrote a test with @DataJpaTest but your repository bean is null during the test. What is the most likely cause?

medium📝 Debug Q6 of 15
Spring Boot - Testing Spring Boot Applications
You wrote a test with @DataJpaTest but your repository bean is null during the test. What is the most likely cause?
AYou used <code>@SpringBootTest</code> instead of <code>@DataJpaTest</code>
BYou forgot to annotate the repository field with <code>@Autowired</code>
CYou did not add <code>@Entity</code> to your entity class
DYou did not include <code>@Test</code> on the test method
Step-by-Step Solution
Solution:
  1. Step 1: Check repository injection

    If the repository field is null, it usually means Spring did not inject it because @Autowired is missing.
  2. Step 2: Verify other options

    Using @SpringBootTest won't cause null injection, missing @Entity causes other errors, and missing @Test does not affect injection.
  3. Final Answer:

    Missing @Autowired on repository field -> Option B
  4. Quick Check:

    Null repo = missing @Autowired [OK]
Quick Trick: Always use @Autowired to inject repositories in tests [OK]
Common Mistakes:
  • Assuming @SpringBootTest causes null injection
  • Confusing entity annotation with injection
  • Thinking missing @Test causes null repo

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes