2014-01-30 28 views
2

我的單元測試和我的android應用程序住在單獨的項目中。如何在Robolectric 2.2中指定自定義/ res目錄?

隨着Robolectric 1,我可以像這樣指定我/ res目錄位置:

public class MyTestRunner extends RobolectricTestRunner { 
    public MyTestRunner(Class<?> testClass) throws InitializationError { 
     super(testClass, "../app/AndroidManifest.xml", "../app/res"); 
    } 
} 

如何在Robolectric 2.2指定/ res目錄的位置?

回答

1

使用RobolectricTestRunner#getAppManifest(Config)

public MyTestRunner(Class<?> testClass) throws InitializationError { 
    super(testClass); 
} 

@Override 
protected AndroidManifest getAppManifest(Config config) { 
    return new AndroidManifest(
      Fs.fileFromPath("../app/AndroidManifest.xml"), 
      Fs.fileFromPath("../app/res"), 
      Fs.fileFromPath("../app/assets")); 
} 
+1

由於Robolectric 2.x中,使用[@Config](http://robolectric.blogspot.com/2013/05/configuring-robolectric-20.html)註釋來代替。 – Xian

+0

@Xian是否'@ Config'用'gradle'構建工作,當測試住在'的src /測試/ java'?還是需要'RobolectricGradleTestRunner'? –

+0

我不清楚爲什麼'gradle'世界如此不同;抱歉,不確定。 – Xian

1

我前一陣子有同樣的問題,所有你需要做的就是與註釋您的測試類:

@Config(manifest = "../app/AndroidManifest.xml")

和依賴性將有所回升從你的清單中爲你提供。

PS。還請確保您已註解測試類@RunWith(RobolectricTestRunner.class)並用適當的JUnit標註(@Before@Test@After等),以及你的方法(以防萬一你沒有忘)。

+2

是否'Robolectric'然後假定'res'目錄是'AndroidManifest.xml'的兄弟(舉例來說,假設'res'是在'../ app')? –

+3

是的,默認情況下它會查找相對於'AndroidManifest.xml'的'res'和'assets'。 請注意,您也可以[指定項目範圍的配置設置(http://robolectric.blogspot.com/2013/05/configuring-robolectric-20.html)就像'的src /測試/資源/組織清單.robolectric.Config.properties': 'manifest = ../app/AndroidManifest.xml' – Xian