You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
Hence I am not sure why ByteBuddy thinks we can mock this type. Can you check whether we indeed hit this check in ByteBuddy and what ByteBuddy returns? That should help us understand why we think we can mock a sealed class, even though we can't.
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:
Then I inject mocked version of OrderRepository:
and when I try to invoke getDepartment():
Department department = orderRepository.getDepartment();
The text was updated successfully, but these errors were encountered: