Skip to content

Commit 27eb8a3

Browse files
authored
Clarify RETURNS_MOCKS behavior with sealed abstract enums (Java 15+) (#3773)
Added documentation note explaining that since Java 15, abstract enums are declared sealed and cannot be mocked. Attempting to return a mock for such types will throw MockitoException instead of returning null. Fixes #3285
1 parent 9e5d449 commit 27eb8a3

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

mockito-core/src/main/java/org/mockito/Mockito.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,10 @@ public class Mockito extends ArgumentMatchers {
18301830
* ReturnsMocks first tries to return ordinary values (zeros, empty collections, empty string, etc.)
18311831
* then it tries to return mocks. If the return type cannot be mocked (e.g. is final) then plain <code>null</code> is returned.
18321832
* <p>
1833+
* <strong>Note:</strong> Since Java 15, abstract enums are declared sealed, which prevents mocking.
1834+
* Attempting to return a mock for such types will throw a {@link org.mockito.exceptions.base.MockitoException}
1835+
* instead of returning <code>null</code>. You can still return an existing enum literal from a stubbed method call.
1836+
* <p>
18331837
*/
18341838
public static final Answer<Object> RETURNS_MOCKS = Answers.RETURNS_MOCKS;
18351839

0 commit comments

Comments
 (0)