我想用 robolectric 3.0-rc2 测试CursorLoader
我的测试班:
@RunWith(RobolectricTestRunner.class)
@Config(emulateSdk = 21)
public class MyCursorLoaderTest {
@Before
public void setUp() {
final ContentProvider contentProvider = new MyProvider();
contentProvider.onCreate();
ShadowContentResolver.registerProvider(MyProvider.AUTHORITY, contentProvider);
}
@Test
public void testCursorLoader(){
CursorLoader cLoader = new CursorLoader(RuntimeEnvironment.application, SomeColumns.CONTENT_URI,
SomeColumns.ALL_COLUMNS, null, null, null);
Cursor c = cLoader.loadInBackground();
Robolectric.flushForegroundScheduler();
assertNotNull(c.getCount());
}
@Test
public void testContentResolver(){
final Cursor c = RuntimeEnvironment.application.getContentResolver().
query(SomeColumns.CONTENT_URI,
SomeColumns.ALL_COLUMNS,
null, null, null);
assertNotNull(c.getCount());
}
}
testContentResolver
成功,但testCursorLoader
失败
堆栈追踪:
java.lang.SecurityException: The authority of the uri content://here.is.authority.of.MyProvider/SomeColumns_Table_Name does not match the one of the contentProvider: null
at android.content.ContentProvider.validateIncomingUri(ContentProvider.java:1795)
at android.content.ContentProvider.access$000(ContentProvider.java:90)
at android.content.ContentProvider$Transport.query(ContentProvider.java:202)
at android.content.ContentResolver.query(ContentResolver.java:478)
at android.content.CursorLoader.loadInBackground(CursorLoader.java:64)
at here.is.project.package.name.controller.fragment.MyCursorLoaderTest.testCursorLoader(MyCursorLoaderTest.java:54)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:235)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:168)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
testCursorLoader
我要改变什么?
答案 0 :(得分:2)
这对我有用:
@Before
public void setUp() {
final ContentProvider contentProvider = new MyProvider();
ProviderInfo providerInfo = new ProviderInfo();
providerInfo.authority = MyProvider.AUTHORITY;
contentProvider.attachInfo(RuntimeEnvironment.application, providerInfo);
contentProvider.onCreate();
ShadowContentResolver.registerProvider(MyProvider.AUTHORITY, contentProvider);
}