Challenge - 5 Problems
Testcontainers Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of JUnit test using Testcontainers PostgreSQL
What is the output of the following JUnit test when run with Testcontainers managing a PostgreSQL database?
JUnit
import org.junit.jupiter.api.Test; import org.testcontainers.containers.PostgreSQLContainer; import static org.junit.jupiter.api.Assertions.*; public class DbTest { static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15.3") .withDatabaseName("testdb") .withUsername("user") .withPassword("pass"); static { postgres.start(); } @Test void testConnection() { String url = postgres.getJdbcUrl(); assertTrue(url.contains("jdbc:postgresql://")); assertEquals("user", postgres.getUsername()); assertEquals("pass", postgres.getPassword()); } }
Attempts:
2 left
💡 Hint
Testcontainers starts the container before tests run and provides valid connection info.
✗ Incorrect
The PostgreSQLContainer starts correctly and provides valid JDBC URL, username, and password matching the configured values. All assertions pass.
❓ assertion
intermediate1:30remaining
Correct assertion to verify Testcontainers MySQL container is running
Which assertion correctly verifies that a MySQL Testcontainer is running during a JUnit test?
JUnit
import org.testcontainers.containers.MySQLContainer; MySQLContainer<?> mysql = new MySQLContainer<>("mysql:8.0"); mysql.start();
Attempts:
2 left
💡 Hint
Check the container's running state with the right method.
✗ Incorrect
The isRunning() method returns true if the container is started and running. Other options check wrong methods or states.
🔧 Debug
advanced2:00remaining
Identify the cause of container startup failure in JUnit test
Given the following JUnit test code using Testcontainers, what is the most likely cause of the test failing with a container startup timeout?
JUnit
import org.junit.jupiter.api.Test; import org.testcontainers.containers.PostgreSQLContainer; public class DbTest { @Test void testDb() { PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:nonexistent-tag") .withDatabaseName("db") .withUsername("user") .withPassword("pass"); postgres.start(); assertTrue(postgres.isRunning()); } }
Attempts:
2 left
💡 Hint
Check the Docker image tag correctness.
✗ Incorrect
Using a non-existent Docker image tag causes the container to fail to start, leading to a timeout error.
🧠 Conceptual
advanced1:30remaining
Why use Testcontainers for database testing in CI pipelines?
Which is the best reason to use Testcontainers for database testing in continuous integration (CI) pipelines?
Attempts:
2 left
💡 Hint
Think about test isolation and environment consistency.
✗ Incorrect
Testcontainers spins up real database containers that are isolated and disposable, ensuring consistent and clean test environments in CI.
❓ framework
expert2:30remaining
Correct JUnit 5 lifecycle usage with Testcontainers PostgreSQL
Which code snippet correctly manages a PostgreSQL Testcontainer lifecycle using JUnit 5 annotations to start once before all tests and stop after all tests?
Attempts:
2 left
💡 Hint
Static methods with @BeforeAll/@AfterAll require static container fields unless using PER_CLASS lifecycle.
✗ Incorrect
JUnit 5 requires @BeforeAll and @AfterAll methods to be static unless the test class uses PER_CLASS lifecycle. Option A correctly uses static container and static lifecycle methods.