Hilt: No JaCoCo test coverage on @AndroidEntryPoint classes.
See original GitHub issueGiven 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()
whereclass ÀctivityBase : AppCompatActivity()
There is coverage reported onActivityBase
but not onMyActivity
.
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:
- Created 3 years ago
- Reactions:2
- Comments:15 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
@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:
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.