A common dilemma while writing unit tests in Java is whether to use Mockito's when/thenReturn or doReturn/when stubbings.

Both when/thenReturn and doReturn/when are used in Mockito, a popular Java testing framework, to mock behavior of methods in an object. However, they are used in slightly different scenarios.

when/thenReturn is used to mock the return value of a method when it is called. Here's an example:

List mockList = mock(List.class);
when(mockList.size()).thenReturn(3);

In the above code, we are creating a mock object of the List class and then using when/thenReturn to mock the behavior of the size() method. When mockList.size() is called, it will return 3.

doReturn/when is used to mock the behavior of a method when it is called, without actually invoking the method. Here's an example:

List mockList = mock(List.class);
doReturn(3).when(mockList).size();

In the above code, we are again creating a mock object of the List class, but this time we are using doReturn/when to mock the behavior of the size() method. When mockList.size() is called, it will not actually invoke the method, but instead return 3.

The difference between the two methods is that when/thenReturn actually invokes the method and returns a mocked value, whereas doReturn/when does not invoke the method and only returns a mocked value.

In general, you should use when/thenReturn to mock the return value of a method, and doReturn/when to mock the behavior of a method.

Furthermore, there are typical cases where doReturn/when is used and other cases where when/thenReturn is preferred.

doReturn/when is typically used when mocking void methods or when you need to stub a method that throws an exception. For example:

List mockList = mock(List.class);
doThrow(new RuntimeException()).when(mockList).clear();

In the above code, we are using doThrow/when to stub the behavior of the clear() method, which is a void method that throws an exception when called.

On the other hand, when/thenReturn is typically used when mocking methods that return a value as in the first example.