0
0
Spring Bootframework~20 mins

Test containers for database testing in Spring Boot - 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!
component_behavior
intermediate
2: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());
    }
}
Ajdbc:postgresql://localhost:5432/testdb
Bjdbc:mysql://<container-ip>:<random-port>/testdb
Cjdbc:postgresql://localhost:<random-port>/testdb
Djdbc:postgresql://localhost:5432/postgres
Attempts:
2 left
💡 Hint
Testcontainers dynamically assigns a random port mapped to localhost.
📝 Syntax
intermediate
2: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'.
A
MySQLContainer&lt;?&gt; mysql = new MySQLContainer&lt;&gt;("mysql:8.0")
    .withDatabaseName("mydb")
    .withUsername("root")
    .withPassword("rootpass");
B
MySQLContainer mysql = new MySQLContainer&lt;&gt;("mysql:8.0")
    .databaseName("mydb")
    .username("root")
    .password("rootpass");
C
;)"ssaptoor"(drowssaPhtiw.    
)"toor"(emanresUhtiw.    
)"bdym"(emaNesabataDhtiw.    
)"0.8:lqsym"(&gt;&lt;reniatnoCLQSyM wen = lqsym &gt;?&lt;reniatnoCLQSyM
D
MySQLContainer mysql = new MySQLContainer&lt;&gt;("mysql:8.0")
    .withDatabaseName("mydb")
    .withUsername("root")
    .withPassword("rootpass");
Attempts:
2 left
💡 Hint
Check the generic type and method names carefully.
🔧 Debug
advanced
2: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?
AThe test method does not have @Transactional annotation.
BThe DataSourceBuilder does not support PostgreSQL connections.
CThe username and password are incorrect because they are hardcoded elsewhere.
DThe container is started too late; it should start before @PostConstruct or statically.
Attempts:
2 left
💡 Hint
Think about when the container starts relative to when the test runs.
state_output
advanced
2: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()'?
Afalse
Btrue
Cnull
DThrows IllegalStateException
Attempts:
2 left
💡 Hint
Stopping a container should update its running state.
🧠 Conceptual
expert
2: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?
ATestcontainers require no Docker installation, making them easier to use.
BTestcontainers provide a real database environment matching production, avoiding differences in SQL dialect and behavior.
CEmbedded databases cannot be used with Spring Boot tests.
DTestcontainers are faster to start and run than embedded databases.
Attempts:
2 left
💡 Hint
Think about how close the test environment is to production.