8000 Robolectric Service Testing · Issue #1969 · google/dagger · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Robolectric Service Testing #1969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
goldy1992 opened this issue Jul 6, 2020 · 2 comments
Closed

Robolectric Service Testing #1969

goldy1992 opened this issue Jul 6, 2020 · 2 comments

Comments

@goldy1992
Copy link

Given a Service class such as:

@AndroidEntryPoint
class MyService() : Service {
  // example dependency
  @Inject
  lateinit var myVariable : String

   // class code
}

When we come to write the Robolectric test e.g.

@RunWith(RobolectricTestRunner::class)
@UninstallModules(...)
@Config(application = HiltTestApplication::class)
@HiltAndroidTest
class MediaPlaybackServiceTest {

  @Rule @JvmField
  val rule : HiltAndroidRule = HiltAndroidRule(this)

  @Before
  fun setup() {
     rule.inject()
     // more setup
  }

  @Test
  fun myTest() {
    val service : MyService = Robolectric.setupService(MyService::class.java)
    // The rest of the test
  }

Expect: To be able to generate an instance of MyService in order to test
Actual: kotlin.UninitializedPropertyAccessException: lateinit property myVariable has not been initialized

It looks like Robolectric is not using Hilt correctly to set up the service.

Relevant sections of the build.gradle

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'

android {
// android code
}

dependencies {
      // hilt
    def HILT_VERSION = '2.28-alpha'
    implementation group: 'com.google.dagger', name: 'hilt-android', version: HILT_VERSION
    kapt group: 'com.google.dagger', name: 'hilt-android-compiler', version: HILT_VERSION

    testImplementation group: 'com.google.dagger', name: 'hilt-android-testing', version: HILT_VERSION
    kaptTest group: 'com.google.dagger', name: 'hilt-android-compiler', version: HILT_VERSION

   def ROBOLECTRIC_VERSION = '4.3.1'
   testImplementation("org.robolectric:robolectric:$ROBOLECTRIC_VERSION") {
        exclude group: "com.google.auto.service", module: "auto-service"
    }
    testImplementation group: 'org.robolectric', name: 'shadows-supportv4', version: ROBOLECTRIC_VERSION
 
  // other depenencies
}
@bcorso
Copy link
bcorso commented Jul 6, 2020

Unfortunately, the bytecode injected classes currently don't get picked up on class path when running Robolectric tests via AndroidStudio. We're hoping to fix this once b/37076369 is fixed.

In the meantime, see #1956 for a couple workarounds.

We also plan to explicitly call this out in the docs. I'm in the process of making those changes now.

@goldy1992
Copy link
Author

I can confirm that the workaround of using the Gradle test runner works and for me suffices in giving reason to close this issue.

Many thanks @bcorso

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0