Bird
Raised Fist0
Spring Bootframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using Testcontainers in Spring Boot database testing?
easy
A. To run real database instances in temporary containers for reliable tests
B. To mock database calls without any real database
C. To speed up unit tests by skipping database interactions
D. To deploy databases permanently on production servers

Solution

  1. Step 1: Understand Testcontainers role

    Testcontainers run real database instances inside temporary containers during tests.
  2. Step 2: Compare with other options

    Mocking or skipping databases is not what Testcontainers do; they provide real environments temporarily.
  3. Final Answer:

    To run real database instances in temporary containers for reliable tests -> Option A
  4. Quick Check:

    Testcontainers purpose = run real temporary databases [OK]
Hint: Testcontainers run real temporary databases, not mocks [OK]
Common Mistakes:
  • Confusing Testcontainers with mocking frameworks
  • Thinking Testcontainers deploy permanent databases
  • Assuming Testcontainers skip database usage
2. Which of the following is the correct way to start a PostgreSQL Testcontainer in a Spring Boot test class?
easy
A. static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:latest").start();
B. PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:latest").run();
C. static PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:latest").begin();
D. PostgreSQLContainer postgres = new PostgreSQLContainer().start();

Solution

  1. Step 1: Check correct instantiation and start method

    The correct way is to create a static container with generic type and call start().
  2. Step 2: Validate options

    static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:latest").start(); uses correct syntax with generic type and start(). Others use wrong method names or miss generics.
  3. Final Answer:

    static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:latest").start(); -> Option A
  4. Quick Check:

    Use start() with generic type for Testcontainers [OK]
Hint: Use start() method and generic type for containers [OK]
Common Mistakes:
  • Using .run() or .begin() instead of .start()
  • Omitting generic type
  • Not declaring container as static when needed
3. Given this Spring Boot test snippet using Testcontainers:
 @Testcontainers
 class MyRepositoryTest {
  @Container
  static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:13").withDatabaseName("testdb").withUsername("user").withPassword("pass");

  @Autowired
  MyRepository repo;

  @Test
  void testCount() {
    long count = repo.count();
    System.out.println(count);
  }
}

What will System.out.println(count); print if the database is empty?
medium
A. null
B. 0
C. Throws NullPointerException
D. 1

Solution

  1. Step 1: Understand repo.count() behavior

    The count() method returns the number of records in the repository's table.
  2. Step 2: Database is empty so count is zero

    Since the database is fresh and empty, count() returns 0, not null or exception.
  3. Final Answer:

    0 -> Option B
  4. Quick Check:

    Empty DB count() = 0 [OK]
Hint: Empty database count returns zero, not null or error [OK]
Common Mistakes:
  • Expecting null instead of zero
  • Assuming count() throws exception on empty DB
  • Confusing count() with find methods
4. You wrote this test using Testcontainers but it fails with org.testcontainers.containers.ContainerLaunchException. What is the likely cause?
 @Container
 static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:latest");

 @Test
 void testDb() {
   // test code
 }
medium
A. The PostgreSQL image name is incorrect
B. The container must be started manually with .start() in the test
C. Docker is not running or accessible on the test machine
D. Testcontainers do not support PostgreSQL

Solution

  1. Step 1: Understand ContainerLaunchException meaning

    This exception usually means Docker cannot start the container.
  2. Step 2: Check common causes

    Most common cause is Docker daemon not running or inaccessible to Testcontainers.
  3. Final Answer:

    Docker is not running or accessible on the test machine -> Option C
  4. Quick Check:

    ContainerLaunchException = Docker not running [OK]
Hint: Check Docker daemon is running if container launch fails [OK]
Common Mistakes:
  • Thinking image name is wrong when it is correct
  • Trying to start container manually when @Container does it
  • Believing Testcontainers don't support PostgreSQL
5. You want to write a Spring Boot integration test using Testcontainers with a MySQL database. You want the container to start once before all tests and stop after all tests. Which setup is correct?
hard
A. Use @SpringBootTest without any container setup; rely on embedded database
B. Create a new MySQLContainer instance inside each test method and call start() manually
C. Use @BeforeEach to start a MySQLContainer and @AfterEach to stop it
D. Declare a static MySQLContainer with @Container annotation in the test class and use @Testcontainers on the class

Solution

  1. Step 1: Understand lifecycle annotations

    Using static container with @Container and @Testcontainers on class starts container once before all tests and stops after all.
  2. Step 2: Compare other options

    Creating container per test or starting/stopping each test is inefficient. Embedded DB is different approach.
  3. Final Answer:

    Declare a static MySQLContainer with @Container annotation in the test class and use @Testcontainers on the class -> Option D
  4. Quick Check:

    Static @Container + @Testcontainers = start once before all tests [OK]
Hint: Use static @Container field with @Testcontainers class annotation [OK]
Common Mistakes:
  • Starting container inside each test method
  • Using @BeforeEach/@AfterEach for container lifecycle
  • Assuming embedded DB replaces Testcontainers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard