Challenge - 5 Problems
Testcontainers Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when running a Spring Boot test with a PostgreSQL Testcontainer?
Given this Spring Boot test setup using Testcontainers for PostgreSQL, what will be the database URL printed during the test?
Spring Boot
import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.testcontainers.containers.PostgreSQLContainer; @SpringBootTest public class DatabaseTest { static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15-alpine") .withDatabaseName("testdb") .withUsername("user") .withPassword("pass"); static { postgres.start(); } @Test void printJdbcUrl() { System.out.println(postgres.getJdbcUrl()); } }
Attempts:
2 left
💡 Hint
Testcontainers dynamically assigns a random port mapped to localhost.
✗ Incorrect
Testcontainers starts a PostgreSQL container with a random free port mapped to container's 5432. The JDBC URL includes localhost and this random port, not fixed ports.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a MySQL Testcontainer in Spring Boot?
Choose the correct Java code snippet to create a MySQL Testcontainer with database name 'mydb', username 'root', and password 'rootpass'.
Attempts:
2 left
💡 Hint
Check the generic type and method names carefully.
✗ Incorrect
Option A uses the correct generic type MySQLContainer> and the proper builder methods with 'with' prefix. Other options miss generics or use incorrect method names.
🔧 Debug
advanced2:00remaining
Why does this Testcontainer-based test fail to connect to the database?
Consider this Spring Boot test code snippet:
@PostConstruct
public void init() {
postgres.start();
}
@Test
void testConnection() {
DataSource ds = DataSourceBuilder.create()
.url(postgres.getJdbcUrl())
.username(postgres.getUsername())
.password(postgres.getPassword())
.build();
try (Connection conn = ds.getConnection()) {
assertFalse(conn.isClosed());
}
}
Why might this test fail with a connection error?
Attempts:
2 left
💡 Hint
Think about when the container starts relative to when the test runs.
✗ Incorrect
Starting the container in @PostConstruct is too late for Spring Boot to configure the DataSource properly. The container should start statically or before the Spring context loads.
❓ state_output
advanced2:00remaining
What is the value of 'container.isRunning()' after calling 'container.stop()'?
Given a running PostgreSQL Testcontainer instance named 'container', what will be the value of 'container.isRunning()' immediately after calling 'container.stop()'?
Attempts:
2 left
💡 Hint
Stopping a container should update its running state.
✗ Incorrect
Calling stop() stops the container and updates its internal state. Therefore, isRunning() returns false after stop().
🧠 Conceptual
expert2:00remaining
Why use Testcontainers for database testing instead of an embedded database?
Which of the following is the best reason to prefer Testcontainers for database testing in Spring Boot over embedded databases like H2?
Attempts:
2 left
💡 Hint
Think about how close the test environment is to production.
✗ Incorrect
Testcontainers run real database instances in Docker containers, ensuring tests run against the same database engine as production. Embedded databases often differ in SQL support and behavior.