Bird
0
0

You wrote a test with @DataJpaTest but it fails with a BeanCreationException related to missing DataSource. What is the most likely cause?

medium📝 Debug Q14 of 15
Spring Boot - Testing Spring Boot Applications
You wrote a test with @DataJpaTest but it fails with a BeanCreationException related to missing DataSource. What is the most likely cause?
AYou did not annotate the test class with @SpringBootTest
BYou forgot to include an in-memory database dependency like H2
CYou used @DataJpaTest on a non-repository class
DYou did not add @Autowired to the repository field
Step-by-Step Solution
Solution:
  1. Step 1: Understand the error cause

    BeanCreationException for DataSource usually means Spring cannot find a database connection.
  2. Step 2: Check dependencies for @DataJpaTest

    @DataJpaTest uses an in-memory database by default, but you must include a dependency like H2 in your project.
  3. Final Answer:

    You forgot to include an in-memory database dependency like H2 -> Option B
  4. Quick Check:

    Missing DB dependency causes DataSource error [OK]
Quick Trick: Add H2 dependency for @DataJpaTest to create DataSource [OK]
Common Mistakes:
  • Confusing @DataJpaTest with @SpringBootTest requirement
  • Ignoring missing database dependencies
  • Assuming @Autowired omission causes DataSource error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes