Bird
0
0

What is the best way to fix this?

hard📝 Application Q8 of 15
Spring Boot - Testing Spring Boot Applications
You want to test a repository method that uses a custom JPQL query with @DataJpaTest. However, the test fails because the query references a table that does not exist. What is the best way to fix this?
AAdd schema.sql or data.sql files to initialize the in-memory database with required tables
BSwitch from <code>@DataJpaTest</code> to <code>@SpringBootTest</code> to load full schema
CAnnotate the test class with <code>@Transactional(propagation = Propagation.NOT_SUPPORTED)</code>
DRemove the custom query and use default repository methods only
Step-by-Step Solution
Solution:
  1. Step 1: Understand in-memory DB setup in @DataJpaTest

    The in-memory database starts empty, so tables must be created via schema or data scripts.
  2. Step 2: Fix missing table error

    Adding schema.sql or data.sql files initializes tables needed for custom queries.
  3. Final Answer:

    Add schema.sql or data.sql files to initialize the in-memory database with required tables -> Option A
  4. Quick Check:

    Initialize schema for in-memory DB to fix missing tables [OK]
Quick Trick: Use schema.sql to create tables in @DataJpaTest in-memory DB [OK]
Common Mistakes:
  • Switching to @SpringBootTest unnecessarily
  • Disabling transactions instead of fixing schema
  • Avoiding custom queries instead of fixing DB

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes