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

單元測(cè)試Android,從資源中獲取字符串

Unit test Android, getString from resource(單元測(cè)試Android,從資源中獲取字符串)
本文介紹了單元測(cè)試Android,從資源中獲取字符串的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

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.

這篇關(guān)于單元測(cè)試Android,從資源中獲取字符串的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

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 操作標(biāo)簽的多行 EditText)
How to detect the swipe left or Right in Android?(如何在 Android 中檢測(cè)向左或向右滑動(dòng)?)
Prevent dialog dismissal on screen rotation in Android(防止在Android中的屏幕旋轉(zhuǎn)對(duì)話框解除)
How do I handle ImeOptions#39; done button click?(如何處理 ImeOptions 的完成按鈕點(diǎn)擊?)
How do you set EditText to only accept numeric values in Android?(您如何將 EditText 設(shè)置為僅接受 Android 中的數(shù)值?)
主站蜘蛛池模板: 亚州一区二区三区 | 亚洲国产成人精品女人久久久 | 日韩国产欧美在线观看 | 亚洲视频一区二区三区 | 亚洲精品在线视频 | 夜夜草导航 | 亚洲欧美成人影院 | 日韩在线小视频 | 亚洲欧美日韩精品久久亚洲区 | 免费人成激情视频在线观看冫 | 91中文在线观看 | 网站国产 | 国产精品99久久久久久大便 | 欧美激情国产日韩精品一区18 | 欧美在线网站 | 午夜伦理影院 | 亚洲精品久久区二区三区蜜桃臀 | 色综合九九 | 久久国产精品一区二区三区 | 全免费a级毛片免费看视频免 | 人人操日日干 | 精品国产欧美 | 欧美乱大交xxxxx另类电影 | 在线观看亚洲精品 | 久久久久免费精品国产小说色大师 | 国产精品色一区二区三区 | 成人毛片一区二区三区 | 密桃av| 中文字幕一区二区三区不卡在线 | 日韩精品在线观看免费 | 成年人视频免费在线观看 | 国产一区二区三区四区三区四 | 日本亚洲一区二区 | av在线影院 | 91精品国产91久久久久久不卞 | 国产精品久久久久久网站 | 粉嫩在线 | 91传媒在线观看 | 亚洲人在线 | 久久久男人的天堂 | 亚洲人成在线观看 |