Unit test Android, getString from resource(单元测试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,从资源中获取字符串
				
        
 
            
        - Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
 - 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
 - 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
 - 用 Swift 实现 UITextFieldDelegate 2022-01-01
 - Android viewpager检测滑动超出范围 2022-01-01
 - android 4中的android RadioButton问题 2022-01-01
 - Android - 拆分 Drawable 2022-01-01
 - MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
 - 想使用ViewPager,无法识别android.support.*? 2022-01-01
 - 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
 
				
				
				
				