Home | History | Annotate | Download | only in basicapi
      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.assertj.core.api.Assertions;
     11 import org.junit.Test;
     12 import org.mockito.Mockito;
     13 import org.mockitoutil.TestBase;
     14 
     15 import static junit.framework.TestCase.assertEquals;
     16 
     17 public class ReplacingObjectMethodsTest extends TestBase {
     18 
     19     private interface DummyInterface {}
     20     private class DummyClass {}
     21 
     22     @Test
     23     public void shouldProvideMockyImplementationOfToString() {
     24         DummyClass dummyClass = Mockito.mock(DummyClass.class);
     25         assertEquals("Mock for DummyClass, hashCode: " + dummyClass.hashCode(), dummyClass.toString());
     26         DummyInterface dummyInterface = Mockito.mock(DummyInterface.class);
     27         assertEquals("Mock for DummyInterface, hashCode: " + dummyInterface.hashCode(), dummyInterface.toString());
     28     }
     29 
     30     @Test
     31     public void shouldReplaceObjectMethods() {
     32         Object mock = Mockito.mock(ObjectMethodsOverridden.class);
     33         Object otherMock = Mockito.mock(ObjectMethodsOverridden.class);
     34 
     35         assertThat(mock).isEqualTo(mock);
     36         assertThat(mock).isNotEqualTo(otherMock);
     37         assertThat(mock.hashCode()).isNotEqualTo(otherMock.hashCode());
     38 
     39         assertThat(mock.toString()).contains("Mock for ObjectMethodsOverridden");
     40     }
     41 
     42     @Test
     43     public void shouldReplaceObjectMethodsWhenOverridden() {
     44         Object mock = Mockito.mock(ObjectMethodsOverriddenSubclass.class);
     45         Object otherMock = Mockito.mock(ObjectMethodsOverriddenSubclass.class);
     46 
     47         assertThat(mock).isEqualTo(mock);
     48         assertThat(mock).isNotEqualTo(otherMock);
     49         assertThat(mock.hashCode()).isNotEqualTo(otherMock.hashCode());
     50 
     51         assertThat(mock.toString()).contains("Mock for ObjectMethodsOverriddenSubclass");
     52     }
     53 
     54     public static class ObjectMethodsOverridden {
     55         public boolean equals(Object o) {
     56             throw new RuntimeException("Should not be called. MethodInterceptorFilter provides implementation");
     57         }
     58         public int hashCode() {
     59             throw new RuntimeException("Should not be called. MethodInterceptorFilter provides implementation");
     60         }
     61         public String toString() {
     62             throw new RuntimeException("Should not be called. MethodInterceptorFilter provides implementation");
     63         }
     64     }
     65 
     66     public static class ObjectMethodsOverriddenSubclass extends ObjectMethodsOverridden {
     67     }
     68 }
     69