Android-针对不同语言环境的自定义字体的单元测试

时间:2018-07-16 07:34:57

标签: android unit-testing robolectric

我正在尝试为应用于不同语言环境的字体创建单元测试。我的字体在res文件夹下的字体目录中。我需要检查是否为选定的ocale应用了正确的字体。我在这些测试中使用Robolectric。但是测试失败了。这是stacktrace。

android.content.res.Resources$NotFoundException: Resource ID #0x7f090002

at android.content.res.Resources.getValue(Resources.java:1013)
at android.support.v4.content.res.ResourcesCompat.loadFont(ResourcesCompat.java:335)
at android.support.v4.content.res.ResourcesCompat.getFont(ResourcesCompat.java:205)
at com.parsifal.starz.ui.features.locale.LocaleTest.arabicLocalTest(LocaleTest.kt:36)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:497)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:228)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:110)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:37)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:64)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

我编写的单元测试是这样的:

@RunWith(RobolectricTestRunner::class)
@Config(constants = BuildConfig::class)
class LocaleTest {

    private lateinit var context: Context

    @Before
    fun setUp() {
        context = RuntimeEnvironment.application
    }


    @Test
    @Config(qualifiers = "ar")
    fun arabicLocalTest() {
        val typeface: Typeface? = ResourcesCompat.getFont(context,  R.font.font_regular)
        assertThat(shadowOf(typeface).getFontDescription().getFamilyName()).isEqualTo("roboto");

    }
}

我是单元测试的新手,是否有任何链接可以指向如何测试自定义字体的方法将提供很大的帮助。我尝试过robolectric guide,但他们使用的都是资产,我需要从 font 文件夹中获取字体。

0 个答案:

没有答案