Subin T
Subin T

Reputation: 157

Android unit testing content resolver class

I want to test the following method in Android. I have used Junit and Mockito.

How do I mock contentResolver.delete method?

Upvotes: 1

Views: 886

Answers (1)

Maciej Kowalski
Maciej Kowalski

Reputation: 26502

Dont think you need to use reflection here.

Try to

1) Set-up the mocked ContentResolver

2) Expect certain method called on LauncherServerCallback

@Test
public void testDeleteUser() throws Exception
{
    // Arrange
    Launcher launcher = new Launcher();
    Mockito.doReturn(1).when(contentResolver).delete(UserProvider.CONTENT_USER_URI, null, null);

    // Act
    launcher.deleteUser(contentResolver,launcherServerCallback);

   // Assert
   Mockito.verify(launcherServerCallback).onSuccess(Mockito.anyString());
}

You may also need to add this to kick-off the Mockito engine:

@Before
public void init(){
   MockitoAnnotations.initMocks(this);
}

Tip: you shouldnt test private methods. So either try to increase the visibiilty or move that code into a separate class.

Upvotes: 2

Related Questions