doReturn() do in JUnit mocking?doReturn() tells a mock object to return a specific value when a method is called. It is useful when you want to avoid calling the real method.
doThrow() work in JUnit mocking?doThrow() makes a mock method throw an exception when called. This helps test how your code handles errors.
doReturn() instead of when().thenReturn() in JUnit?doReturn() is safer when mocking methods that are final, private, or have side effects. It avoids calling the real method during setup.
doThrow() usage in JUnit.doThrow(new RuntimeException()).when(mockedObject).someMethod();
This makes someMethod() throw a RuntimeException when called.
doReturn() and thenReturn() in JUnit mocking?thenReturn() is used with when() and calls the real method during setup, which can cause issues. doReturn() does not call the real method and is safer for certain cases.
doReturn(value).when(mock).method() do?doReturn() sets the return value for the mocked method call.
doThrow() makes the mock throw an exception on method call.
doReturn() over when().thenReturn()?doReturn() avoids calling the real method, which is safer for some methods.
doThrow(new Exception()).when(mock).method()?The mock method throws the exception when called.
doReturn()?doReturn() avoids calling the real method; calling it is a reason to avoid when().thenReturn().
doReturn() and doThrow() help in mocking with JUnit.doReturn() is better than when().thenReturn().