相关疑难解决方法(0)

单元测试Android,来自资源的getString

我正在尝试为Android应用程序进行单元测试,我需要从res.string资源获取一个字符串.我想测试的类是POJO类.我正在用两种语言做应用程序,因此,我需要从资源中获取一个字符串.问题是我无法获得上下文或活动,是否可能?我知道使用Instrumentation测试我可以做到,但我需要在进行仪器测试(黑盒测试)之前测试一些功能(白盒测试).这是我必须测试的功能:

public void setDiaByText(String textView) {
    getll_diaSeleccionado().clear();
    if (textView.contains(context.getResources().getString(R.string.sInicialLunes))) {
        getll_diaSeleccionado().add(0);
        getIsSelectedArray()[0] = true;
        getI_idiaSeleccionado()[0] =1;

    } else
    {
        getIsSelectedArray()[0] = false;
        getI_idiaSeleccionado()[0] =0;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是测试:

@Test
public void setDiaByTextView() {
    String texto = "L,M,X,J,V,S,D";

    alertaPOJO.setDiaByText(texto);

    assertEquals(alertaPOJO.getIsSelectedArray()[0], true);
    assertEquals(alertaPOJO.getI_idiaSeleccionado()[0], 1);
}
Run Code Online (Sandbox Code Playgroud)

尝试时会崩溃 context.getResources().getString(R.string.sInicialLunes))

如果我把'Mon'代替context.getResources().getString(R.string.sInicialLunes))或'L'它完全正常工作,是否可以获取上下文或活动以访问资源文件夹?

我正在使用Mockito进行测试,setUp函数是:

@Before
public void setUp() throws Exception {

    MockitoAnnotations.initMocks(this);
    mContext = Mockito.mock(Alerta.class);
    Mockito.when(mContext.getApplicationContext()).thenReturn(mContext);

    alertaPOJO = new AlertaPOJO();
}
Run Code Online (Sandbox Code Playgroud)

谢谢

testing junit android mockito

12
推荐指数
4
解决办法
1万
查看次数

标签 统计

android ×1

junit ×1

mockito ×1

testing ×1