Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso: How to use R.string resources of androidTest folder

I want to put data in xml file of androidTest/res/values/string.xml folder. I have created the file say test.xml with below contents.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="test">hello</string>
</resources>

When i am trying to access the field test via R.string.test. It is not accessible and says "cannot resolve symbol test". Can someone please advice me here.

like image 889
Bingo Avatar asked Apr 30 '16 13:04

Bingo


Video Answer


2 Answers

androidx.test.platform.app.InstrumentationRegistry is deprecated use androidx.test.core.app.ApplicationProvider

val targetContext = ApplicationProvider.getApplicationContext<Context>()
val test: String =  targetContext.resources.getString(R.string.test)
like image 78
Jitesh Mohite Avatar answered Sep 21 '22 13:09

Jitesh Mohite


This is how you do it:

import com.my.example.test.R;

Resources resources = InstrumentationRegistry.getContext().getResources();
String terms = resources.getString(R.string.test);
like image 34
IgorGanapolsky Avatar answered Sep 22 '22 13:09

IgorGanapolsky