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:
Step 1: Check correct container declaration
Testcontainers require @Container annotation on a static field for lifecycle management; the image name must be valid.
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.
Final Answer:
@Container
static PostgreSQLContainer> postgres = new PostgreSQLContainer<>("postgres:latest"); -> Option B
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
Master "Testing Spring Boot Applications" in Spring Boot
9 interactive learning modes - each teaches the same concept differently