question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hilt: No JaCoCo test coverage on @AndroidEntryPoint classes.

See original GitHub issue

Given a class

@AndroidEntryPoint
MyActivity : AppCompatActivity()

and a test class

@HiltAndroidTest
@RunWith(RobolectricTestRunner::class)
@LooperMode(LooperMode.Mode.PAUSED)
class MyActivityTest() {
    private lateinit var scenario : ActivityScenario<MainActivity>

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

    @Before
    fun setup() {
        rule.inject()
        scenario = ActivityScenario.launch(MyActivity::class.java)
    }

    @Test
    fun myActivityTest() {
        scenario.onActivity { activity: MyActivity ->
        // test code
        }
   }
  // .. rest of test class
}

and given JaCoCo Gradle Plugin version 0.8.5

Expected: Some code coverage on MyActivity class. Actual:: 0% coverage

Notes:

  • The coverage worked fine before the hilt plugin migration
  • Given the same scenario with @AndroidEntryPoint MyActivity : MyActivityBase() where class ÀctivityBase : AppCompatActivity() There is coverage reported on ActivityBase but not on MyActivity.

This is surely to do with the fact that Hilt is adding an injecting class between MyActivity and it’s super class.

This is a Generic example, please tag me if you require any more information.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
goldy1992commented, Nov 13, 2020

A useful note for anyone having trouble with this using Android Gradle Plugin (AGP) 4.1. In order to get any unit test code coverage with JaCoCo you need to testCoverageEnabled false so that AGP’s compiled classes for use on device are not used instead of the local instrumented classes.

See https://issuetracker.google.com/issues/171125857 and AGP 4.1 release notes on unit tests https://developer.android.com/studio/releases/gradle-plugin

2reactions
goldy1992commented, Oct 17, 2020

@Chang-Eric this does indeed provide a workaround… Due to the vague explanation in the link you provided, I will demonstrate this with an example for an activity:

@AndroidEntryPoint(AppCompatActivity::class)
class MainActivity : Hilt_MainActivity() {
 // contents of class
}

@HiltAndroidTest
@RunWith(RobolectricTestRunner::class)
@LooperMode(LooperMode.Mode.PAUSED)
class MainActivityTest {

    private lateinit var scenario : ActivityScenario<MainActivity>

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

    @Before
    fun setup() {
        rule.inject()
        scenario = ActivityScenario.launch(MainActivity::class.java)
    }

    @Test
    fun myTest() {
    }
}

This declaration allows JaCoCo to know of the existence of the generated Hilt_MainActivity class… This is a workaround as we end up having to refer to generated code from our real code, which doesn’t seem to cause to much trouble on my Android Studio 4.0.1 however will probably cause havoc on previous versions and/or other IDEs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hilt: No JaCoCo test coverage on @AndroidEntryPoint classes.
The coverage worked fine before the hilt plugin migration; Given the same scenario with @AndroidEntryPoint MyActivity : MyActivityBase() where class ...
Read more >
Dagger Hilt: Testing injected Android components with code ...
With Hilt, Robolectric, Espresso, and JaCoCo, we can test actual Android objects that are injected with Dagger with code coverage. This allows ...
Read more >
Jacoco code coverage 0% in Android-kotlin project
7; gradle plugin 7.0.2. I am finding that if debug { testCoverageEnabled true } is set, jacoco produces no output for unit tests...
Read more >
Dagger Hilt Learnings - dead fish
Code Coverage. If your @AndroidEntryPoint s don't show up in Jacoco's code coverage reports as covered, even though you have tests for them, ......
Read more >
Dagger Hilt in Android with Example - GeeksforGeeks
First, we annotate it with @AndroidEntryPoint annotation which will make the component class(activity, fragments, views, services, and broadcast ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found