2015-08-09 88 views
0

我無法在本地單元測試中使用android.net.Uri(在開發計算機上的Android Studio中運行)。我知道這是因爲單元測試是針對一個「精簡」的android.jar來執行的,而stub只會拋出異常,但是這使得測試與資源相關的任何東西(文件,網址等)變得不可能。Android Studio單元測試 - android.net.Uri缺失

我有同樣的問題,測試時使用JSON代碼,以及爲了解決它,我加入我的build.gradle依賴:

testCompile「org.json:JSON:20140107」

任何類似的android.net.Uri解決方案?

回答

0

完整的答案是:

  1. 添加插件到您的buildscript依賴關係:

    buildscript { 
        repositories { 
         jcenter() 
        } 
        dependencies { 
         classpath 'de.mobilej.unmock:UnMockPlugin:0.6.0' 
        }} 
    
  2. 而這款以模塊的構建腳本:

    應用插件:「去.mobilej.unmock'

  3. 添加您想要k的類eep

    unMock keepStartingWith「libcore」。 保持 「android.net.Uri」 keepAndRename 「java.nio.charset.Charsets」 到 「xjava.nio.charset.Charsets」 }

    依賴{}

這裏是用於按預期運行的測試代碼

public class WearableUtilsTest { 

    @Test 
    public void testUri() throws Exception { 
     final int nodeId = 1000; 
     final String resourcePath = Params.WEATHER; 
     Uri wearableResourceUri = DataWearInteractor.getUri(nodeId, resourcePath); 
     assertEquals("Uri should be equal", "wear://" + nodeId + resourcePath, wearableResourceUri.getPath()); 
    } 
}