Bird
0
0

Which of the following is the correct way to declare a PostgreSQL Testcontainer field in a Spring Boot test class?

easy📝 Syntax Q3 of 15
Spring Boot - Testing Spring Boot Applications
Which of the following is the correct way to declare a PostgreSQL Testcontainer field in a Spring Boot test class?
A@Container static PostgreSQLContainer postgres = new PostgreSQLContainer("postgresql");
B@Container static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:latest");
C@Container PostgreSQLContainer postgres = new PostgreSQLContainer("mysql:latest");
D@Testcontainers PostgreSQLContainer postgres = new PostgreSQLContainer();
Step-by-Step Solution
Solution:
  1. Step 1: Check correct container declaration

    Testcontainers require @Container annotation on a static field for lifecycle management; the image name must be valid.
  2. Step 2: Validate image and syntax

    @Container static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:latest"); uses correct image "postgres:latest" and static field with @Container annotation.
  3. Final Answer:

    @Container static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:latest"); -> Option B
  4. Quick Check:

    Correct declaration = static @Container with valid image [OK]
Quick Trick: Use static @Container with valid image name [OK]
Common Mistakes:
  • Missing static keyword on container field
  • Using wrong image name for PostgreSQL
  • Confusing @Testcontainers with @Container

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes