0
0
Spring Bootframework~10 mins

Test containers for database testing in Spring Boot - 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.

Spring Boot
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("[1]:latest");
postgres.start();
Drag options to blanks, or click blank then click option'
Apostgres
Bpostgresql:latest
Cpostgresql
Dpostgres:latest
Attempts:
3 left
💡 Hint
Common Mistakes
Using just 'postgres' without tag
Using incorrect image name like 'postgresql' instead of 'postgres'
2fill in blank
medium

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

Spring Boot
String jdbcUrl = postgres.[1]();
Drag options to blanks, or click blank then click option'
AgetJdbcUrl
BjdbcUrl
CgetJdbcURL
DgetUrl
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method name 'getJdbcURL' (case sensitive)
Using 'getUrl' which returns container URL, not JDBC URL
3fill in blank
hard

Fix the error in the annotation to use Testcontainers with JUnit 5.

Spring Boot
@[1]
public class MyDatabaseTest {
    // test code
}
Drag options to blanks, or click blank then click option'
AEnableTestcontainers
BTestcontainers
CExtendWith(TestcontainersExtension.class)
DRunWith(Testcontainers.class)
Attempts:
3 left
💡 Hint
Common Mistakes
Using JUnit 4 annotation @RunWith
Using @Testcontainers without extension
4fill in blank
hard

Fill both blanks to define a Spring Boot test configuration that uses the container's JDBC URL and credentials.

Spring Boot
@DynamicPropertySource
static void datasourceProperties(DynamicPropertyRegistry registry) {
    registry.add("spring.datasource.url", () -> [1]);
    registry.add("spring.datasource.username", () -> [2]);
}
Drag options to blanks, or click blank then click option'
Apostgres.getJdbcUrl()
Bpostgres.getUsername()
Cpostgres.getPassword()
Dpostgres.getUrl()
Attempts:
3 left
💡 Hint
Common Mistakes
Using getUrl() instead of getJdbcUrl()
Using getPassword() for username
5fill in blank
hard

Fill all three blanks to create a Map of properties for Spring Boot datasource configuration using the container.

Spring Boot
Map<String, String> props = Map.of(
    "spring.datasource.url", [1],
    "spring.datasource.username", [2],
    "spring.datasource.password", [3]
);
Drag options to blanks, or click blank then click option'
Apostgres.getJdbcUrl()
Bpostgres.getUsername()
Cpostgres.getPassword()
Dpostgres.getUrl()
Attempts:
3 left
💡 Hint
Common Mistakes
Using getUrl() instead of getJdbcUrl()
Mixing up username and password methods