Wanted but not invoked: Mockito sharedPreferences when multiple tests are there

407 views Asked by At

I have written 2 test cases which will test 2 functions of a class which does similar kind of functiomality.

This is my PrefHelper class in project:

public String getID() {
    if(ID==null) {
        ID = sharedpreferences.getString(_ID, null);
    }
    return ID ;
}

public void setID(String ID ) {
    prefsEditor.putString(_ID,ID );
    prefsEditor.apply();
    this.ID = ID ;
}

public String getApiBasePath() {
    if(apiBasePath==null) {
        apiBasePath = sharedpreferences.getString(API_BASE_PATH, null);
    }
    return apiBasePath;
}

public void setApiBasePath(String apiBasePath) {
    prefsEditor.putString(API_BASE_PATH,apiBasePath);
    prefsEditor.apply();
    this.apiBasePath = apiBasePath;
}

And following is my test class:

@Mock
Context context;

@Mock
SharedPreferences sharedPreferences;

@Mock
SharedPreferences.Editor editor;

private  PrefsHelper prefsHelper;

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    when(context.getSharedPreferences(anyString(),anyInt())).thenReturn(sharedPreferences);
    when(sharedPreferences.edit()).thenReturn(editor);
    when(sharedPreferences.getString(anyString(),isNull(String.class))).thenReturn(null);
    when(editor.putString(anyString(),anyString())).thenReturn(editor);
    prefsHelper =  PrefsHelper.getInstance(context);

}


@Test
public void apiBasePathTest(){

    String returnNull=prefsHelper.getApiBasePath();
    verify(sharedPreferences,times(1)).getString(anyString(),isNull(String.class));
    assertEquals(null,returnNull);


    doNothing().when(editor).apply();
    final ArgumentCaptor<String> stringsCaptor =
            ArgumentCaptor.forClass(String.class);
    prefsHelper.setApiBasePath("basePath");
    verify(editor,times(1)).putString(anyString(),stringsCaptor.capture());
    verify(editor,times(1)).apply();
    assertEquals("basePath", stringsCaptor.getAllValues().get(0));

    String returnNonNull=prefsHelper.getApiBasePath();
    verify(sharedPreferences,times(1)).getString(anyString(),isNull(String.class));
    assertEquals("basePath",returnNonNull);


}


@Test
public void IDTest(){
    String returnNull=prefsHelper.getID();
    assertEquals(null,returnNull);
    verify(sharedPreferences,times(1)).getString(anyString(),isNull(String.class));

    doNothing().when(editor).apply();
    final ArgumentCaptor<String> stringsCaptor =
            ArgumentCaptor.forClass(String.class);
    prefsHelper.setID("ID");
    verify(editor,times(1)).putString(anyString(),stringsCaptor.capture());
    verify(editor,times(1)).apply();
    assertEquals("ID", stringsCaptor.getAllValues().get(0));

    String returnNonNull=prefsHelper.getID();
    verify(sharedPreferences,times(1)).getString(anyString(),isNull(String.class));
    assertEquals("ID",returnNonNull);

}

First test case runs fine, it fails in second test at all the verify method. Its assert true works fine but verify fails in all cases and I get the below error:

Wanted but not invoked:
sharedPreferences.getString(<any>, isNull());
-> at PrefsHelperTest.IDTest

Actually, there were zero interactions with this mock. I am surprised that it works fine on first test but fails on second, where both test are doing similar functionality.

0

There are 0 answers