我只是想測試一下,獲取一個字符串資源等於我認爲它應該等於什麼。我的問題似乎是我的項目中有Realm。我知道Robolectric不支持Realm(它在文檔中聲明),但我根本不調用Realm,所以我覺得可能有辦法做到這一點。Android - 用Robolectric測試一個字符串資源
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import static org.junit.Assert.assertEquals;
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21, manifest = "app/src/main/AndroidManifest.xml")
public class ResourceTester {
@Test
public void testingString() {
String resourceString = RuntimeEnvironment.application.getString(R.string.app_name);
assertEquals(resourceString, "Yeah");
}
}
它看起來像它IS試圖調用境界
java.lang.UnsatisfiedLinkError: Can't load library:
/var/folders/3x/ddxtg5fs1hxgqrp6h63vsc140000gp/T/android-tmp-
robolectric3612430387389787158/app_lib/librealm-jni.dylib.3.5.0
編輯:我嘗試做更多的事情,而且好像在@Config註釋設置manifest
是問題,但後來我得到一個android.content.res.Resources$NotFoundException: unknown resource 2131362072
任何其他的想法?我可以創建另一個應用程序類,其中Realm
未被調用嗎?/test目錄如何知道?
編輯大衛:
我嘗試這樣做:
@RunWith(RobolectricGradleTestRunner.class)
@Config(application = TestingApplication.class, constants = BuildConfig.class, sdk = 21)
public class ResourceTester {
@Test
public void newTestingTests() throws Exception {
String appName = RuntimeEnvironment.application.getString(R.string.app_name);
}
}
但我得到一個:
android.content.res.Resources$NotFoundException: unknown resource 2131362072
如果我將其更改爲
@RunWith(RobolectricTestRunner.class)
//@RunWith(RobolectricGradleTestRunner.class)
@Config(application = TestingApplication.class, constants = BuildConfig.class, sdk = 21)
public class ResourceTester {
@Test
public void newTestingTests() throws Exception {
String appName = RuntimeEnvironment.application.getString(R.string.app_name);
}
}
我得到
WARNING: No manifest file found at ./AndroidManifest.xml.Falling back to the Android OS resources only.
To remove this warning, annotate your test class with @Config(manifest=Config.NONE).
android.content.res.Resources$NotFoundException: unknown resource 2131362072
Robolectric是什麼版本? – EpicPandaForce
@EpicPandaForce我目前正在使用3.0的robolectric,但我可以使用任何版本3.0及以上。 – EGHDK
您是否有Robolectric調用的自定義應用程序類,然後該類在測試期間調用Realm.init(Context)? – EpicPandaForce