Bird
0
0

You want to initialize a MySQL Testcontainer with a custom schema and data before running Spring Boot integration tests. Which method correctly applies the SQL initialization?

hard📝 Application Q8 of 15
Spring Boot - Testing Spring Boot Applications
You want to initialize a MySQL Testcontainer with a custom schema and data before running Spring Boot integration tests. Which method correctly applies the SQL initialization?
AUse Spring's <code>@Sql</code> annotation without configuring the container
BManually execute SQL scripts inside the test method after container startup
CMount a volume with SQL scripts and use <code>withInitScript("schema.sql")</code> on the MySQLContainer instance
DRely on MySQL default schema without initialization
Step-by-Step Solution
Solution:
  1. Step 1: Use withInitScript

    Testcontainers' MySQLContainer supports withInitScript to run SQL scripts on startup.
  2. Step 2: Evaluate other options

    Manually execute SQL scripts inside the test method after container startup is manual and error-prone; C does not affect container DB; D ignores initialization.
  3. Final Answer:

    Mount a volume with SQL scripts and use withInitScript("schema.sql") on the MySQLContainer instance -> Option C
  4. Quick Check:

    Use withInitScript for container DB initialization [OK]
Quick Trick: Use withInitScript() to run SQL on container startup [OK]
Common Mistakes:
  • Running SQL scripts manually inside tests
  • Assuming @Sql affects container initialization
  • Ignoring schema setup leading to test failures

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes