Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test Android, getString from resource

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

like image 843
Huan Avatar asked Sep 19 '16 15:09

Huan


People also ask

How do you write JUnit test cases for Android?

You write your local unit test class as a JUnit 4 test class. To do so, create a class that contains one or more test methods, usually in module-name/src/test/ . A test method begins with the @Test annotation and contains the code to exercise and verify a single aspect of the component that you want to test.

How do you write unit test cases in Kotlin?

Test structure Each test should be created from the following blocks: Arrange/Given - in which we will prepare all needed data required to perform test. Act/When - in which we will call single method on tested object. Assert/Then - in which we will check result of the test, either pass or fail.


2 Answers

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.

like image 77
Michal Harakal Avatar answered Sep 21 '22 09:09

Michal Harakal


You don't have a real android Context while you are using JVM unit test. For your case, maybe you can try Android Instrumentation Test, typically it is implemented in the "androidTest" directory of your project.

like image 31
Sherlock Avatar answered Sep 21 '22 09:09

Sherlock