11package nl .jqno .equalsverifier .internal .instantiation ;
22
3+ import static org .mockito .Mockito .doReturn ;
34import static org .mockito .Mockito .mock ;
45import static org .mockito .Mockito .withSettings ;
56
67import java .util .Optional ;
78
89import nl .jqno .equalsverifier .internal .exceptions .MockitoException ;
9- import nl .jqno .equalsverifier .internal .instantiation .Attributes ;
1010import nl .jqno .equalsverifier .internal .reflection .Tuple ;
1111import nl .jqno .equalsverifier .internal .reflection .TypeTag ;
1212
@@ -45,8 +45,8 @@ public <T> Optional<Tuple<T>> provide(TypeTag tag, Attributes attributes) {
4545 }
4646
4747 try {
48- var red = buildMock (type , attributes .fieldName ());
49- var blue = buildMock (type , attributes .fieldName ());
48+ var red = buildMock (type , attributes .fieldName (), "red" );
49+ var blue = buildMock (type , attributes .fieldName (), "blue" );
5050 if (!red .equals (blue ) && red .hashCode () != blue .hashCode ()) {
5151 // Only return mocked values if they're properly unequal.
5252 // They should be, but I think this is undocumented behaviour, so best to be safe.
@@ -55,16 +55,19 @@ public <T> Optional<Tuple<T>> provide(TypeTag tag, Attributes attributes) {
5555 }
5656 catch (RuntimeException ignored ) {
5757 // I would prefer to catch MockitoException, but that leads to class loading errors in modules that don't have Mockito
58+ ignored .printStackTrace ();
5859 return Optional .empty ();
5960 }
6061
6162 return Optional .empty ();
6263 }
6364
64- private <T > T buildMock (Class <T > type , String fieldName ) {
65- return mock (type , withSettings ().defaultAnswer (invocation -> {
65+ private <T > T buildMock (Class <T > type , String fieldName , String color ) {
66+ var m = mock (type , withSettings ().defaultAnswer (invocation -> {
6667 // Throw an exception on any method except equals and hashCode
6768 throw new MockitoException (fieldName , type .getSimpleName (), invocation .getMethod ().getName ());
6869 }));
70+ doReturn ("[" + color + " mock for " + type .getSimpleName () + "]" ).when (m ).toString ();
71+ return m ;
6972 }
7073}
0 commit comments