ContentValues ​​method is not mocking

I am creating a test with Mockito. In the test, I create an object of type ContentValues. When I run this test, I get an error message:

java.lang.RuntimeException: Method put in android.content.ContentValues not mocked.

Here is the minimum code:

import android.content.ContentValues;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class MyClassTest {
    @Test
    public void test1() {
        ContentValues cv = new ContentValues();
        cv.put("key", "value");
    }
}

What to do to avoid this error?

+4
source share
1 answer

You are using a bullying library that has no implementations. Since your test actually calls a method on an object, without using a mocking library to give it behavior, it gives you this message.

As on the Android device testing support page :

"Method ... do not scoff."

android.jar, , , Android . ( ). , , - Android ( , , Mockito). , build.gradle, :

android {
  // ...
  testOptions { 
    unitTests.returnDefaultValues = true
  }
}

, , ​​ Mockito, , put, Robolectric Java, .

+13

All Articles