Bird
0
0

Given this test snippet:

medium📝 component behavior Q13 of 15
Spring Boot - Testing Spring Boot Applications
Given this test snippet:
@MockBean
private PaymentService paymentService;

@Test
void testProcess() {
  when(paymentService.pay()).thenReturn(true);
  boolean result = paymentService.pay();
  System.out.println(result);
}

What will be printed when the test runs?
Anull
Bfalse
Ctrue
DException thrown
Step-by-Step Solution
Solution:
  1. Step 1: Understand mocking behavior

    The mock paymentService is told to return true when pay() is called using when().thenReturn(true).
  2. Step 2: Trace the method call

    Calling paymentService.pay() returns true as configured, so result is true and printed.
  3. Final Answer:

    true -> Option C
  4. Quick Check:

    Mock returns configured value = true [OK]
Quick Trick: Mock returns what you tell it with when().thenReturn() [OK]
Common Mistakes:
  • Expecting default false instead of configured true
  • Confusing mock with real implementation output
  • Not setting up when().thenReturn() before call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes