0
0
JUnittesting~20 mins

Test containers for database testing in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Testcontainers Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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());
    }
}
ATest fails because getJdbcUrl() returns null
BTest fails with container startup timeout exception
CTest fails due to username mismatch assertion error
DTest passes successfully with all assertions true
Attempts:
2 left
💡 Hint
Testcontainers starts the container before tests run and provides valid connection info.
assertion
intermediate
1: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();
AassertTrue(mysql.isRunning());
BassertEquals(true, mysql.isCreated());
CassertFalse(mysql.isRunning());
DassertNull(mysql.getJdbcUrl());
Attempts:
2 left
💡 Hint
Check the container's running state with the right method.
🔧 Debug
advanced
2: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());
    }
}
AThe database name 'db' is invalid and prevents container startup
BThe Docker image tag 'postgres:nonexistent-tag' does not exist, causing startup failure
CThe username and password are missing and cause authentication failure
DThe test method is missing @BeforeAll annotation to start the container
Attempts:
2 left
💡 Hint
Check the Docker image tag correctness.
🧠 Conceptual
advanced
1: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?
AIt slows down tests by requiring full OS virtualization
BIt requires manual installation of databases on CI servers
CIt provides a lightweight, disposable database instance isolated per test run
DIt uses in-memory databases that do not support real SQL features
Attempts:
2 left
💡 Hint
Think about test isolation and environment consistency.
framework
expert
2: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?
A
import org.junit.jupiter.api.*;
import org.testcontainers.containers.PostgreSQLContainer;

public class DbTest {
    static PostgreSQLContainer&lt;?&gt; postgres = new PostgreSQLContainer&lt;&gt;("postgres:15.3");

    @BeforeAll
    static void startContainer() {
        postgres.start();
    }

    @AfterAll
    static void stopContainer() {
        postgres.stop();
    }

    @Test
    void test() {
        Assertions.assertTrue(postgres.isRunning());
    }
}
B
import org.junit.jupiter.api.*;
import org.testcontainers.containers.PostgreSQLContainer;

public class DbTest {
    static PostgreSQLContainer&lt;?&gt; postgres = new PostgreSQLContainer&lt;&gt;("postgres:15.3");

    @BeforeEach
    void startContainer() {
        postgres.start();
    }

    @AfterEach
    void stopContainer() {
        postgres.stop();
    }

    @Test
    void test() {
        Assertions.assertTrue(postgres.isRunning());
    }
}
C
import org.junit.jupiter.api.*;
import org.testcontainers.containers.PostgreSQLContainer;

@TestInstance(TestInstance.Lifecycle.PER_METHOD)
public class DbTest {
    PostgreSQLContainer&lt;?&gt; postgres = new PostgreSQLContainer&lt;&gt;("postgres:15.3");

    @BeforeAll
    void startContainer() {
        postgres.start();
    }

    @AfterAll
    void stopContainer() {
        postgres.stop();
    }

    @Test
    void test() {
        Assertions.assertTrue(postgres.isRunning());
    }
}
D
import org.junit.jupiter.api.*;
import org.testcontainers.containers.PostgreSQLContainer;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class DbTest {
    PostgreSQLContainer&lt;?&gt; postgres = new PostgreSQLContainer&lt;&gt;("postgres:15.3");

    @BeforeAll
    void startContainer() {
        postgres.start();
    }

    @AfterAll
    void stopContainer() {
        postgres.stop();
    }

    @Test
    void test() {
        Assertions.assertTrue(postgres.isRunning());
    }
}
Attempts:
2 left
💡 Hint
Static methods with @BeforeAll/@AfterAll require static container fields unless using PER_CLASS lifecycle.