久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

單元測試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,從資源中獲取字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event(EditText:禁用文本選擇處理程序單擊事件上的粘貼/替換菜單彈出)
Multiline EditText with Done SoftInput Action Label on 2.3(2.3 上帶有完成 SoftInput 操作標簽的多行 EditText)
How to detect the swipe left or Right in Android?(如何在 Android 中檢測向左或向右滑動?)
Prevent dialog dismissal on screen rotation in Android(防止在Android中的屏幕旋轉對話框解除)
How do I handle ImeOptions#39; done button click?(如何處理 ImeOptions 的完成按鈕點擊?)
How do you set EditText to only accept numeric values in Android?(您如何將 EditText 設置為僅接受 Android 中的數值?)
主站蜘蛛池模板: 亚洲性视频 | 欧美激情五月 | 欧美精品免费观看二区 | 精品伊人久久 | 国产在线精品一区二区 | 国产精品免费一区二区三区四区 | 日韩和的一区二区 | 国产三区视频在线观看 | 伊人在线 | 欧美日韩在线成人 | 国产成人99久久亚洲综合精品 | 在线视频 中文字幕 | 国精品一区 | 欧美精品福利视频 | 玖玖玖在线观看 | 一区二区在线不卡 | 精品视频国产 | 亚洲精品视频一区 | 欧美激情综合色综合啪啪五月 | 中文字幕亚洲专区 | 91传媒在线观看 | 亚州春色| 亚洲国产高清高潮精品美女 | 视频在线一区二区 | 国产特级毛片aaaaaa | 久久久久久久一区 | 欧美日韩在线电影 | 久久久久久久久久久久久9999 | 国产激情视频 | 成人免费视频在线观看 | 亚洲在线一区 | 国产片网站| 亚洲国产精品久久 | 国产精品久久久久久婷婷天堂 | 天天操狠狠操 | 国产一区二区免费电影 | 久久小视频 | 在线观看黄色 | 欧美日韩精品 | 日韩视频在线一区 | 国产最新视频在线 |