Mockito mock object using real implementation

Viewed 3211

Why is mockMap using the real implementation? How do I prevent this?

In method testFirstKeyMatch

when(mockMap.keySet().toArray()[0])...

throws ArrayIndexOutOfBoundsException: 0 when running the test.

MaxSizeHashMap is a LinkedHashMap with max size of 7, throws an IndexOutOfBoundsException when I attempt to put more into.

Profile keeps track of something not essential for this.

SuperClass.java

public class SuperClass {

protected String[] days;
protected MaxSizeHashMap<String, String> map;

public SuperClass() {
    days = new String[7];
    map = new MaxSizeHashMap<String, String>();
    //...
}

void updateDays() {

    cal = Calendar.getInstance();

    for (int i = 0; i < 7; i = i + 1) {

        //adds short names "Mon", "Tue", ... to days
        days[i] = cal.getDisplayName(Calendar.DAY_OF_WEEK, 
                Calendar.SHORT, Locale.US);

        cal.add(Calendar.DATE, 1);
    }
}

void firstKeyMatch(Profile profile) {

    updateDays(); 

    //checks if first key of map is not same as days[0]
    if (days[0] != map.keySet().toArray()[0]) {

        profile.add();

        //...
     }
 }
 }

SuperClassTest.java

@RunWith(MockitoJUnitRunner.class)
public class SuperClassTest {

@InjectMocks
private SuperClass spr = new SuperClass();

@Mock
private MaxSizeHashMap<String, String> mockMap;

@Mock
private Profile mockProfile;

//...

@Test
public void testFirstKeyMatch() {   

    when(mockMap.keySet().toArray()[0]).thenReturn(spr.days[0]);

    verify(mockProfile, never()).add();

}
}
2 Answers
Related