Skip to content

Incorrect documentation for RETURNS_MOCKS #3285

@sergey-morenets

Description

@sergey-morenets

Hi

I checked the latest (5.11.0) documentation for Mockito class (https://javadoc.io/static/org.mockito/mockito-core/5.11.0/org/mockito/Mockito.html#23) and it states:
ReturnsMocks first tries to return ordinary values (zeros, empty collections, empty string, etc.) then it tries to return mocks. If the return type cannot be mocked (e.g. is final) then plain null is returned.

I have an enumeration Department with abstract methods (Mockito is unable to mock it). Let's say it's used in OrderRepository:

public interface OrderRepository {
	
	Department getDepartment();
}

Then I inject mocked version of OrderRepository:

@ExtendWith(MockitoExtension.class)
public class OrderServiceTest {

	@Mock(answer = Answers.RETURNS_MOCKS)
	OrderRepository orderRepository;

and when I try to invoke getDepartment():
Department department = orderRepository.getDepartment();

I suppose to receive null value according to the documentation but get an error:
org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: class Department.
Sealed abstract enums can't be mocked. Since Java 15 abstract enums are declared sealed, which prevents mocking.
You can still return an existing enum literal from a stubbed method call.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions