0
0
JUnittesting~20 mins

Answer interface for dynamic responses in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dynamic Response Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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"));
    }
}
Aworld\nunknown
Bhello\ntest
Cunknown\nunknown
Dworld\nworld
Attempts:
2 left
💡 Hint
Focus on how the dynamic answer checks the input argument and returns different strings.
assertion
intermediate
1: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";
});
AassertNull(mockService.respond("go"));
BassertTrue(mockService.respond("go").equals("fail"));
CassertEquals("success", mockService.respond("go"));
DassertFalse(mockService.respond("go").equals("success"));
Attempts:
2 left
💡 Hint
Check what the dynamic answer returns for input "go".
🔧 Debug
advanced
2: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);
ABecause anyString() matcher does not allow null arguments.
BBecause thenAnswer cannot handle null return values.
CBecause mockService is not properly initialized.
DBecause arg is null and calling arg.equals("test") throws NullPointerException.
Attempts:
2 left
💡 Hint
Consider what happens when you call a method on a null object.
framework
advanced
1: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?
AthenAnswer()
BthenReturn()
CdoThrow()
Dverify()
Attempts:
2 left
💡 Hint
This feature uses a callback to compute the return value.
🧠 Conceptual
expert
2: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?
AIt prevents any exceptions from being thrown during test execution.
BIt allows the mock to return different values based on input parameters, enabling more realistic and flexible test scenarios.
CIt improves test execution speed by caching responses.
DIt automatically generates test data without any input from the tester.
Attempts:
2 left
💡 Hint
Think about how dynamic behavior helps simulate real method calls.