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.bugs; 7 8 import org.junit.Test; 9 import org.mockitoutil.TestBase; 10 11 import static junit.framework.TestCase.assertEquals; 12 import static org.mockito.Mockito.*; 13 14 //see issue 101 15 public class CovariantOverrideTest extends TestBase { 16 17 public interface ReturnsObject { 18 19 Object callMe(); 20 } 21 22 public interface ReturnsString extends ReturnsObject { 23 24 // Java 5 covariant override of method from parent interface 25 String callMe(); 26 } 27 28 @Test 29 public void returnFoo1() { 30 ReturnsObject mock = mock(ReturnsObject.class); 31 when(mock.callMe()).thenReturn("foo"); 32 assertEquals("foo", mock.callMe()); // Passes 33 } 34 35 @Test 36 public void returnFoo2() { 37 ReturnsString mock = mock(ReturnsString.class); 38 when(mock.callMe()).thenReturn("foo"); 39 assertEquals("foo", mock.callMe()); // Passes 40 } 41 42 @Test 43 public void returnFoo3() { 44 ReturnsObject mock = mock(ReturnsString.class); 45 when(mock.callMe()).thenReturn("foo"); 46 assertEquals("foo", mock.callMe()); // Passes 47 } 48 49 @Test 50 public void returnFoo4() { 51 ReturnsString mock = mock(ReturnsString.class); 52 mock.callMe(); // covariant override not generated 53 ReturnsObject mock2 = mock; // Switch to base type to call covariant override 54 verify(mock2).callMe(); // Fails: java.lang.AssertionError: expected:<foo> but was:<null> 55 } 56 } 57