Skip to content

Commit a95612c

Browse files
authored
Merge pull request #1088 from jqno/improve-mockito-error-message
Improves MockitoException error message
2 parents 9ba897e + ab6072e commit a95612c

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818

1919
- Support for Kotlin delegates. ([Issue 1083](https://github.com/jqno/equalsverifier/issues/1083))
2020

21+
### Changed
22+
23+
- When a Mockito error occurs, the error message now says which mocked method was attempted to call. ([Issue 1082](https://github.com/jqno/equalsverifier/issues/1082))
24+
2125
## [4.0.1] - 2025-06-10
2226

2327
### Fixed

equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/exceptions/MockitoException.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
import nl.jqno.equalsverifier.internal.util.Formatter;
44

55
public class MockitoException extends MessagingException {
6-
public MockitoException(String fieldName, String typeName) {
6+
public MockitoException(String fieldName, String typeName, String methodName) {
77
super(Formatter
88
.of(
99
"""
10-
Unable to use Mockito to mock field %% of type %%.
11-
Please provide a prefab value for this type, or use #set(Mode.skipMockito()) to skip using Mockito altogether.""",
10+
Unable to use Mockito to mock field %%:
11+
tried to call method %%.%%, which EqualsVerifier can't mock.
12+
Please provide a prefab value for type %%,
13+
or use #set(Mode.skipMockito()) to skip using Mockito altogether.""",
1214
fieldName,
15+
typeName,
16+
methodName,
1317
typeName)
1418
.format());
1519
}

equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/MockitoValueProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public <T> Optional<Tuple<T>> provide(TypeTag tag, String fieldName) {
6363
private <T> T buildMock(Class<T> type, String fieldName) {
6464
return mock(type, withSettings().defaultAnswer(invocation -> {
6565
// Throw an exception on any method except equals and hashCode
66-
throw new MockitoException(fieldName, type.getSimpleName());
66+
throw new MockitoException(fieldName, type.getSimpleName(), invocation.getMethod().getName());
6767
}));
6868
}
6969
}

equalsverifier-test-mockito/src/test/java/nl/jqno/equalsverifier/mockito/MockitoTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ void verifyClassThatCallsDirectlyIntoField() {
3939
.verify())
4040
.assertFailure()
4141
.assertCause(MockitoException.class)
42-
.assertMessageContains("Unable to use Mockito to mock field methodCaller of type PojoWithoutEquals.");
42+
.assertMessageContains(
43+
"Unable to use Mockito to mock field methodCaller",
44+
"tried to call method PojoWithoutEquals.getI");
4345
}
4446

4547
@Test
@@ -64,7 +66,9 @@ void verifyClassThatContainsSparseArrayThatCallsMethods() {
6466
.when(() -> EqualsVerifier.forClass(SparseArrayEqualsContainer.class).verify())
6567
.assertFailure()
6668
.assertCause(MockitoException.class)
67-
.assertMessageContains("Unable to use Mockito to mock field sparseArray of type SparseArray");
69+
.assertMessageContains(
70+
"Unable to use Mockito to mock field sparseArray",
71+
"tried to call method SparseArray.size");
6872
}
6973

7074
@Test

0 commit comments

Comments
 (0)