Challenge - 5 Problems
Dynamic Response Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this JUnit test using dynamic response interface?
Consider a JUnit test that uses a dynamic answer interface to return different values based on input arguments. What will be the printed output after running this test?
JUnit
import org.junit.jupiter.api.Test; import org.mockito.stubbing.Answer; import static org.mockito.Mockito.*; interface Service { String respond(String input); } public class DynamicAnswerTest { @Test void testDynamicAnswer() { Service mockService = mock(Service.class); when(mockService.respond(anyString())).thenAnswer((Answer<String>) invocation -> { String arg = invocation.getArgument(0); return arg.equals("hello") ? "world" : "unknown"; }); System.out.println(mockService.respond("hello")); System.out.println(mockService.respond("test")); } }
Attempts:
2 left
💡 Hint
Focus on how the dynamic answer checks the input argument and returns different strings.
✗ Incorrect
The dynamic answer checks if the input argument equals "hello" and returns "world"; otherwise, it returns "unknown". So for input "hello", output is "world"; for "test", output is "unknown".
❓ assertion
intermediate1:30remaining
Which assertion correctly verifies a dynamic response from a mocked method?
Given a mocked service with a dynamic answer returning "success" for input "go" and "fail" otherwise, which assertion correctly verifies the response for input "go"?
JUnit
Service mockService = mock(Service.class); when(mockService.respond(anyString())).thenAnswer(invocation -> { String arg = invocation.getArgument(0); return arg.equals("go") ? "success" : "fail"; });
Attempts:
2 left
💡 Hint
Check what the dynamic answer returns for input "go".
✗ Incorrect
The dynamic answer returns "success" when input is "go", so the assertion must check for "success" equality.
🔧 Debug
advanced2:00remaining
Why does this dynamic answer cause a NullPointerException?
Examine the following code snippet. Why does calling mockService.respond(null) cause a NullPointerException?
JUnit
Service mockService = mock(Service.class); when(mockService.respond(null)).thenAnswer(invocation -> { String arg = invocation.getArgument(0); if (arg.equals("test")) { return "ok"; } else { return "fail"; } }); mockService.respond(null);
Attempts:
2 left
💡 Hint
Consider what happens when you call a method on a null object.
✗ Incorrect
The argument passed is null, so calling arg.equals("test") causes a NullPointerException because you cannot call methods on null.
❓ framework
advanced1:00remaining
Which Mockito feature allows dynamic responses based on method arguments?
In Mockito, which feature lets you define a method's return value dynamically depending on the input arguments during test execution?
Attempts:
2 left
💡 Hint
This feature uses a callback to compute the return value.
✗ Incorrect
thenAnswer() allows you to provide a custom Answer implementation that can inspect arguments and return values dynamically.
🧠 Conceptual
expert2:30remaining
What is the main advantage of using an Answer interface for dynamic responses in unit tests?
Why would a tester prefer using the Answer interface to provide dynamic responses in mocks instead of fixed return values?
Attempts:
2 left
💡 Hint
Think about how dynamic behavior helps simulate real method calls.
✗ Incorrect
Using Answer lets the mock respond differently depending on inputs, which helps simulate complex behaviors and edge cases in tests.