Skip to content
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

EarlyComponent is not created if test contains @BindValue #2789

Closed
ReginFell opened this issue Jul 28, 2021 · 3 comments · Fixed by #2791
Closed

EarlyComponent is not created if test contains @BindValue #2789

ReginFell opened this issue Jul 28, 2021 · 3 comments · Fixed by #2791

Comments

@ReginFell
Copy link

ReginFell commented Jul 28, 2021

In our project we have a Service which is init automatically.

class PushNotificationService : FirebaseMessagingService() {
    
    @EarlyEntryPoint
    @InstallIn(SingletonComponent::class)
    interface PushNotificationServiceInterface {   }

    override fun onMessageReceived(message: RemoteMessage) {
       EarlyEntryPoints.get(application, PushNotificationServiceInterface::class.java) <-- Crash here
    }
}

And test

@HiltAndroidTest
class Journey {

   @get:Rule
    val rule = RuleChain.outerRule(HiltAndroidRule(this))
        .around(ActivityScenarioRule(MainActivity::class.java))

   @BindValue val userRepository = mock<UserRepository>()

   @Test
    fun test() {
    }
}

And in gradle we add kaptAndroidTest(hilt)

In this case if we run the test we receive:

The EarlyComponent was requested but does not exist. Check that you have annotated your test class with @HiltAndroidTest and that the processor is running over your test.

But if we remove the @BindView

@HiltAndroidTest
class Journey {

    @get:Rule
    val rule = RuleChain.outerRule(HiltAndroidRule(this))
        .around(ActivityScenarioRule(MainActivity::class.java))

    @Test
    fun test() {
    }
}

Everything works as expected and EarlyComponent can be retrieved.

@bcorso
Copy link

bcorso commented Jul 28, 2021

Hi @ReginFell,

Sorry, I couldn't reproduce this locally. Is it possible for you to create an example project that reproduces the issue?

(Also, on an unrelated note, you may want to read the warning about using ActivityScenarioRule with @BindValue in https://dagger.dev/hilt/testing#bind-value).

@ReginFell
Copy link
Author

ReginFell commented Jul 29, 2021

@bcorso yeah sure.
https://github.com/ReginFell/HiltEarlyComponentIssue

To reproduce you can run
com.regin.earlycomponentissue.HiltEarlyComponentIssueTest

For me when I run the test, PushNotificationService.onNewToken() throws

java.lang.RuntimeException: The EarlyComponent was requested but does not exist. Check that you have annotated your test class with @HiltAndroidTest and that the processor is running over your test.


And if you remove

@BindValue
val test2 = "Hello2"

Everything works just fine

@bcorso
Copy link

bcorso commented Jul 29, 2021

Thanks for the repro case!

I was able to reproduce this issue. I'll have a fix out shortly, but you'll have to wait until the next Hilt release to get it.

Fwiw, this only happens when all of your tests have test-specific bindings (e.g. an @BindValue). Thus, a quick workaround is to add an empty test with no @BindValue in the same source set, for example:

// This test can be removed with the next Hilt release,
// which contains the fix for https://github.com/google/dagger/issues/2789
@HiltAndroidTest
class EmptyTest {}

copybara-service bot pushed a commit that referenced this issue Jul 30, 2021
…bleAggregatingTask`.

Due to the missing package in the aggregated packages list, the Hilt Gradle plugin was silently dropping `EarlyEntryPoint` metadata when `enableAggregatingTask = true`.
See #2789

Added a new Gradle module as a regression test.

Fixes #2789

RELNOTES=Fix #2789: Fixes issue with using EarlyEntryPoints being dropped when using `enableAggregatingTask`
PiperOrigin-RevId: 387690285
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants