Bird
0
0

Given this Spring Boot test snippet using Testcontainers:

medium📝 component behavior Q13 of 15
Spring Boot - Testing Spring Boot Applications
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?
Anull
B0
CThrows NullPointerException
D1
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes