title: How to mock a Jetpack Datastore in a Espresso
body: <p>I switched from SharedPreferences to Jetpack DataStore.</p>
  <p>I am unable to mock the data from the datastore call in the Instrumentation test</p>
  <p>In declaration</p>
  <pre><code> val dataStore = context.createDataStore(name = &quot;App Name&quot;)
  
  </code></pre>
  <p>In the declaration get a string from preferences</p>
  <pre><code>   suspend fun getString(
          key: Preferences.Key&lt;String&gt;,
          defaultVal: String,
          context: Context
      ): String {
          return dataStore?.data?.catch {
              if (it is IOException) {
                  it.printStackTrace()
                  emit(emptyPreferences())
              } else {
                  throw it
              }
          }?.map {
              it[key] ?: defaultVal
          }?.first() ?: &quot;&quot;
      }
  </code></pre>
  <p>In the usage</p>
  <pre><code> SharedPreferenceHelper.getString(
                  Preferences.Key&lt;T&gt;,
                  &quot;&quot;,
                  requireContext()
              )
  </code></pre>
  <p>Manipulate datastore preferences string key to get mocked value in instrumentation test as the desired value.</p>
  <p>Thank you in advance.</p>