Robolectric:从资产创建时,字体对象都是相同的

时间:2017-02-08 11:35:27

标签: android unit-testing robolectric

我有自定义字体,我想测试,我不想使用Android测试,因为他们需要一个模拟器/设备来获取AssetManager。所以我想使用Robolectric,但我遇到了这个奇怪的问题(通过单元测试说明):

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 25, shadows = ShadowMultiDex.class)
public class CustomFontUnitTest {

    @Test
    public void typefacesShouldNotBeIdentical() throws Exception {
        AssetManager assets = RuntimeEnvironment.application.getAssets();

        Typeface avenirLight = Typeface.createFromAsset(assets, "fonts/avenir_lt_35_light.ttf");
        Typeface avenirLightOblique = Typeface.createFromAsset(assets, "fonts/avenir_lt_35_light_oblique.ttf");

        assertThat(avenirLight, not(equalTo(avenirLightOblique)));
    }
}

我正在使用:

testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.9.5'
testCompile 'org.robolectric:robolectric:3.2.2'
testCompile 'org.robolectric:shadows-multidex:3.2.2'

但是当我运行测试时,Typeface对象是相同的:

java.lang.AssertionError: 
Expected: not <android.graphics.Typeface@3fd1>
     but: was <android.graphics.Typeface@3fd1>
Expected :not <android.graphics.Typeface@3fd1>

有没有人知道我可能做错了什么?

1 个答案:

答案 0 :(得分:1)

我重构了我的代码,以便参数是字体名称(String)而不是实际的Typeface。现在我不再需要上下文或字体,并且可以编写简单的jvm单元测试:)