我只是想测试获取 String 资源是否等于我认为它应该等于的值。我的问题似乎是我的项目中有领域。我知道 Robolectric 不支持 Realm(它在文档中说明),但我根本没有调用 Realm,所以我觉得可能有办法做到这一点。
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");
}
}
看起来它确实在尝试调用Realm
java.lang.UnsatisfiedLinkError: Can't load library:
/var/folders/3x/ddxtg5fs1hxgqrp6h63vsc140000gp/T/android-tmp-
robolectric3612430387389787158/app_lib/librealm-jni.dylib.3.5.0
编辑:我尝试了更多的东西,似乎manifest
在 @Config 注释中设置是问题,但后来我得到了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