In JUnit testing, what is the main purpose of using a mock double?
Think about how mocks help check if certain methods were called.
Mocks simulate real objects and allow verification of method calls and interactions, which helps ensure the tested code behaves correctly.
Which statement best describes the difference between a stub and a fake test double?
Consider which double has actual logic and which just returns fixed data.
Stubs return fixed data for testing, while fakes have simple working implementations that behave like the real object but are easier to use.
What will be the output of the following JUnit test using Mockito when the verification fails?
import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; class ServiceTest { @Test void testServiceCall() { Service mockService = mock(Service.class); // No method call on mockService verify(mockService).performAction(); } } interface Service { void performAction(); }
Think about what happens if you verify a method call that never happened.
Mockito throws a verification error if a method expected to be called was never invoked on the mock.
Given a stubbed method in JUnit using Mockito that returns a fixed value, which assertion correctly verifies the returned value?
import static org.mockito.Mockito.*; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; class CalculatorTest { @Test void testAdd() { Calculator calc = mock(Calculator.class); when(calc.add(2, 3)).thenReturn(5); int result = calc.add(2, 3); // Which assertion is correct here? } } interface Calculator { int add(int a, int b); }
Check the expected value from the stubbed method.
The stubbed method returns 5 for inputs 2 and 3, so the assertion must check for 5.
In a complex integration test involving a database and external API, which test double is most appropriate to isolate the external API while keeping realistic behavior for the database?
Consider which double provides simple working behavior and which verifies interactions.
Fakes provide simple working implementations suitable for external APIs in integration tests, while using the real database keeps realistic data behavior.