3

我想測試CursorLoaderrobolectric 3.0-RC2如何測試的Android CursorLoader與robolectric

我的測試類:

@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

它不應該是必要的創建和註冊您的內容提供商。它已經由Robolectric完成了。所以刪除你的setIp()方法。而不是RobolectricTestRunner嘗試RobolectricGradleTestRunner。 – nenick

回答

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); 
} 
相關問題