0
0
JUnittesting~10 mins

Test containers for database testing in JUnit - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a PostgreSQL test container.

JUnit
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:latest").[1]();
Drag options to blanks, or click blank then click option'
Arun
Bstart
Claunch
Dinit
Attempts:
3 left
💡 Hint
Common Mistakes
Using run() or launch() instead of start().
2fill in blank
medium

Complete the code to get the JDBC URL from the running container.

JUnit
String jdbcUrl = postgres.[1]();
Drag options to blanks, or click blank then click option'
AretrieveUrl
BgetUrl
CfetchJdbc
DgetJdbcUrl
Attempts:
3 left
💡 Hint
Common Mistakes
Using getUrl() which does not return the JDBC URL.
3fill in blank
hard

Fix the error in the annotation to use the PostgreSQL container as a JUnit 5 extension.

JUnit
@RegisterExtension
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:latest").[1]();
Drag options to blanks, or click blank then click option'
Astart
BwithDatabaseName
CwithReuse
DwithInitScript
Attempts:
3 left
💡 Hint
Common Mistakes
Calling start() manually when using @RegisterExtension.
4fill in blank
hard

Fill both blanks to create a test container with a specific database name and username.

JUnit
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:latest")
    .[1]("testdb")
    .[2]("testuser");
Drag options to blanks, or click blank then click option'
AwithDatabaseName
BwithUsername
CwithPassword
DwithInitScript
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up withPassword() and withUsername().
5fill in blank
hard

Fill all three blanks to create a test container with a database name, username, and password.

JUnit
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:latest")
    .[1]("mydb")
    .[2]("admin")
    .[3]("secret");
Drag options to blanks, or click blank then click option'
AwithDatabaseName
BwithUsername
CwithPassword
DwithInitScript
Attempts:
3 left
💡 Hint
Common Mistakes
Using withInitScript() instead of withPassword().