androidtestingjunitmockito

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


Solution

  • If you use MockK it's the same.

    @RunWith(AndroidJUnit4::class)
    class YourClassUnitTest : TestCase() {
    
        @MockK
        private lateinit var resources: Resources
    
        @Before
        public override fun setUp() {
            MockKAnnotations.init(this)
        }
    
        @Test
        fun test() {
            every {
                resources.getQuantityString(R.plurals.age, YEARS, YEARS)
            } returns AGE
            every {
                resources.getString(
                    R.string.surname,
                    SURNAME
                )
            } returns TITLE
    
            // Assume you test this method that returns data class 
            // (fields are calculated with getQuantityString and getString)
            val data = getData(YEARS, SURNAME)
    
            assertEquals(AGE, data.age)
            assertEquals(TITLE, data.title)
        }
    
        companion object {
            const val YEARS = 10
            const val AGE = "$YEARS years"
            const val SURNAME = "Johns"
            const val TITLE = "Mr. $SURNAME"
        }
    }
    

    See also Skip a parameter in MockK unit test, Kotlin to get a result of string resources for any data.