11

Expected

Importing libraries such as androidx.test:core:1.2.0, androidx.test.ext:truth:1.2.0, com.google.truth:truth:0.44, and etc. into a local JUnit test class named ExampleUnitTest.kt.

Observed

Error

Unresolved reference: test

Implementation

ExampleUnitTest.kt

import androidx.test.core.app.ApplicationProvider.getApplicationContext
import com.google.common.truth.Truth.assertThat
AdamHurwitz
  • 9,758
  • 10
  • 72
  • 134
  • Are you using this in an instrumeted test (`androidTest/`) or a unit test (`test/`)? [You seem to have the right package](https://androidx.tech/packages/androidx.test.core.app/#applicationprovider), but perhaps your Gradle dependency directive does not match where the test class resides. – CommonsWare Aug 11 '19 at 23:36
  • @CommonsWare, this is under the 'src/test' directory for JUnit testing. – AdamHurwitz Aug 11 '19 at 23:44
  • @CommonsWare, I fixed the first issue with the solution I posted. However, now when I try to retrieve the **app** context, using `InstrumentationRegistry.getInstrumentation().getTargetContext()` or `getApplicationContext()` I receive an error _java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation._. This is using the libraries `import androidx.test.platform.app.InstrumentationRegistry` and `import androidx.test.core.app.ApplicationProvider.getApplicationContext`. – AdamHurwitz Aug 11 '19 at 23:46
  • AFAIK, `ApplicationProvider` is for instrumented tests, at least at the moment. I think there's a plan to unify these things with Mockito-based mocks for unit tests as part of Project Nitrogen, but I don't think that's ready yet. – CommonsWare Aug 11 '19 at 23:48
  • What about `InstrumentationRegistry.getInstrumentation().getTargetContext()`? I've read this should be used to access the **app** context within a JUnit test [here](https://stackoverflow.com/a/52932361/2253682). – AdamHurwitz Aug 11 '19 at 23:54
  • @CommonsWare, I've sent a [note](https://twitter.com/AdamSHurwitz/status/1160703815609225216) to '@AndroidDev' via Twitter regarding the documentation. – AdamHurwitz Aug 12 '19 at 00:07

1 Answers1

30

Solution

This appears to be an issue documented in the Android Testing Codelab sample app.

Within the Codelab sample build.gradle this is referred to as a known issue.

// Once https://issuetracker.google.com/127986458 is fixed this can be testImplementation
implementation "androidx.test:core:$androidXTestCoreVersion"

I refactored my app's dependency to testImplementation instead of androidTestImplementation.

testImplementation 'androidx.test:core:1.2.0'
testImplementation 'androidx.test.ext:truth:1.2.0'
testImplementation 'com.google.truth:truth:0.44'

Note - The Google documentation Set up project for AndroidX Test should be updated to reflect this issue as it currently instructs the developer to use androidTestImplementation. I have filed a documentation issue to resolve this.

AdamHurwitz
  • 9,758
  • 10
  • 72
  • 134