Reputation: 6462
Say my ContentProvider is called DogProvider. How do I set up my Instrumentation test skeleton? When I try to run the following, I always end up with a null MockContentResolver.
import org.junit.Test;
import org.junit.runner.RunWith;
import android.content.ContentProvider;
import android.database.Cursor;
import android.net.Uri;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.filters.LargeTest;
import android.test.ProviderTestCase2;
import android.support.test.InstrumentationRegistry;
import android.test.mock.MockContentResolver;
import com.bus.proj.data.DogContract;
import com.bus.proj.data.DogProvider;
@RunWith(AndroidJUnit4.class)
@LargeTest
public class ContentProviderTest extends ProviderTestCase2<DogProvider>{
public ContentProviderTest() {
super(DogProvider.class, DogContract.CONTENT_AUTHORITY);
}
@Override
protected void setUp() throws Exception{
setContext(InstrumentationRegistry.getTargetContext());
super.setUp();
}
@Test
public void emptyQuery(){
MockContentResolver contentResolver = getMockContentResolver();
assertNotNull(contentResolver);//fail happens here
Uri uri = DogContract.DogEntry.CONTENT_URI;
Cursor cursor = contentResolver.query(uri,null,null,null,null);
}
}
Upvotes: 1
Views: 888
Reputation: 10203
In your test you're using the AndroidJunit4
test runner, which is based on annotation (compared to the JUnit3 which was based on method names).
This means that your setUp method is probably not called. For it to be called before each test, you need to use the @Before annotation, and make your method public :
@Before
@Override
public void setUp() throws Exception{
setContext(InstrumentationRegistry.getTargetContext());
super.setUp();
}
Upvotes: 1