1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 
6 package org.mockitousage.basicapi;
7 
8 import static org.assertj.core.api.Assertions.assertThat;
9 
10 import org.junit.Test;
11 import org.mockito.Mockito;
12 import org.mockitoutil.TestBase;
13 
14 import static org.junit.Assert.assertEquals;
15 
16 public class ReplacingObjectMethodsTest extends TestBase {
17 
18     private interface DummyInterface {}
19     private class DummyClass {}
20 
21     @Test
shouldProvideMockyImplementationOfToString()22     public void shouldProvideMockyImplementationOfToString() {
23         DummyClass dummyClass = Mockito.mock(DummyClass.class);
24         assertEquals("Mock for DummyClass, hashCode: " + dummyClass.hashCode(), dummyClass.toString());
25         DummyInterface dummyInterface = Mockito.mock(DummyInterface.class);
26         assertEquals("Mock for DummyInterface, hashCode: " + dummyInterface.hashCode(), dummyInterface.toString());
27     }
28 
29     @Test
shouldReplaceObjectMethods()30     public void shouldReplaceObjectMethods() {
31         Object mock = Mockito.mock(ObjectMethodsOverridden.class);
32         Object otherMock = Mockito.mock(ObjectMethodsOverridden.class);
33 
34         assertThat(mock).isEqualTo(mock);
35         assertThat(mock).isNotEqualTo(otherMock);
36         assertThat(mock.hashCode()).isNotEqualTo(otherMock.hashCode());
37 
38         assertThat(mock.toString()).contains("Mock for ObjectMethodsOverridden");
39     }
40 
41     @Test
shouldReplaceObjectMethodsWhenOverridden()42     public void shouldReplaceObjectMethodsWhenOverridden() {
43         Object mock = Mockito.mock(ObjectMethodsOverriddenSubclass.class);
44         Object otherMock = Mockito.mock(ObjectMethodsOverriddenSubclass.class);
45 
46         assertThat(mock).isEqualTo(mock);
47         assertThat(mock).isNotEqualTo(otherMock);
48         assertThat(mock.hashCode()).isNotEqualTo(otherMock.hashCode());
49 
50         assertThat(mock.toString()).contains("Mock for ObjectMethodsOverriddenSubclass");
51     }
52 
53     public static class ObjectMethodsOverridden {
equals(Object o)54         public boolean equals(Object o) {
55             throw new RuntimeException("Should not be called. MethodInterceptorFilter provides implementation");
56         }
hashCode()57         public int hashCode() {
58             throw new RuntimeException("Should not be called. MethodInterceptorFilter provides implementation");
59         }
toString()60         public String toString() {
61             throw new RuntimeException("Should not be called. MethodInterceptorFilter provides implementation");
62         }
63     }
64 
65     public static class ObjectMethodsOverriddenSubclass extends ObjectMethodsOverridden {
66     }
67 }
68