单元测试Android,从资源中获取字符串

Unit test Android, getString from resource(单元测试Android,从资源中获取字符串)
本文介绍了单元测试Android,从资源中获取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

I am trying to do an unit test for an android app and I need to get a string from res.string resources. The class that I want to test is a POJO class. I am doing the app in two languages, due to this, I need to get a string from resource. The problem is that I cannot get the context or the activity, is possible? I know that with Instrumentation test I can do it, but I need to test some functions (white box test) before to do the instrumentation test (black box test). This is the function that I have to test:

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;
    }
}

And this is the test:

@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);
}

It crash when try to do context.getResources().getString(R.string.sInicialLunes))

If I put 'Mon' instead of context.getResources().getString(R.string.sInicialLunes)) or 'L' it work perfectly so, is possible to get the context or the activity in order to access to resource folder?

I am testing with Mockito and the setUp function is:

@Before
public void setUp() throws Exception {

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

    alertaPOJO = new AlertaPOJO();
}

Thanks

解决方案

If you are using Context only for obtaining String resource, I would go by mocking only getResources().getString() part like this (see JUnit4 notation):

@RunWith(MockitoJUnitRunner.class)
public class AlertaPOJOTest {

  @Mock
  Context mMockContext;

  @Test
  public void setDiaByTextView() {
     String texto = "L,M,X,J,V,S,D";
     when(mMockContext.getString(R.string.sInicialLunes))
       .thenReturn(INITIAL_LUNES);


     alertaPOJO.setDiaByText(texto);

     assertEquals(alertaPOJO.getIsSelectedArray()[0], true);
     assertEquals(alertaPOJO.getI_idiaSeleccionado()[0], 1);
   } 
}

There are many reasons to stay with JVM tests, most important one, they are running quicker.

这篇关于单元测试Android,从资源中获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Android Mocking a Dagger2 injected dependency for a Espresso test(Android 为 Espresso 测试模拟 Dagger2 注入依赖项)
Mock object in Android Unit test with kotlin - any() gives null(使用 kotlin 在 Android 单元测试中模拟对象 - any() 给出 null)
How to mock Context using Mockito?(如何使用 Mockito 模拟上下文?)
Mockito - what does verify method do?(Mockito - 验证方法有什么作用?)
Unit Testing in Retrofit for Callback(回调改造中的单元测试)
Android Unit Test with Retrofit2 and Mockito or Robolectric(使用 Retrofit2 和 Mockito 或 Robolectric 进行 Android 单元测试)