Bird
0
0

Consider this test snippet using @DataJpaTest:

medium📝 Predict Output Q5 of 15
Spring Boot - Testing Spring Boot Applications
Consider this test snippet using @DataJpaTest:
@Test
void testCount() {
  long countBefore = repo.count();
  repo.save(new User(null, "Alice"));
  long countAfter = repo.count();
  System.out.println(countAfter - countBefore);
}
What will be printed?
A0
B1
C-1
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand repo.count() before and after save

    Initially, countBefore is the number of users. After saving one user, countAfter increases by 1.
  2. Step 2: Calculate difference

    The difference is 1, so printing countAfter - countBefore outputs 1.
  3. Final Answer:

    1 -> Option B
  4. Quick Check:

    Count increases by one after save [OK]
Quick Trick: Saving adds one entity, increasing count by 1 [OK]
Common Mistakes:
  • Assuming count does not change
  • Confusing countBefore and countAfter
  • Expecting compilation errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes