when().thenReturn() do in JUnit testing?when().thenReturn() is used to tell a mock object what value to return when a specific method is called. It helps simulate behavior for testing.
getName() of a mock object userMock to return "Alice"?when(userMock.getName()).thenReturn("Alice");Stubbing lets you control the output of dependencies so you can test your code in isolation without relying on real objects or external systems.
By default, the mock returns default values like null, 0, or false. It does not execute real code.
thenReturn() be used to return different values on consecutive calls?Yes, you can chain thenReturn() calls to return different values each time the method is called.
when(mock.method()).thenReturn(1, 2);
when().thenReturn() in JUnit?when().thenReturn() is used to stub methods on mocks, telling them what to return when called.
The correct syntax is when(mock.method()).thenReturn(value);
Mocks return default values like null or 0 if not stubbed.
You can chain thenReturn() to return different values each call.
when().thenReturn() for mocking in JUnit tests?when().thenReturn() is part of Mockito, a mocking framework used with JUnit.
when().thenReturn() helps in unit testing with mocks.