Package dagger.hilt.android.testing
Class HiltAndroidRule
java.lang.Object
dagger.hilt.android.testing.HiltAndroidRule
- All Implemented Interfaces:
org.junit.rules.TestRule
A
TestRule
for Hilt that can be used with JVM or Instrumentation tests.
This rule is required. The Dagger component will not be created without this test rule.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionorg.junit.runners.model.Statement
apply
(org.junit.runners.model.Statement baseStatement, org.junit.runner.Description description) Completes Dagger component creation ifdelayComponentReady()
was used.Delays creating the component untilcomponentReady()
is called.void
inject()
Completes Dagger injection.
-
Constructor Details
-
HiltAndroidRule
Creates a new instance of the rules. Tests should passthis
.
-
-
Method Details
-
apply
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement baseStatement, org.junit.runner.Description description) - Specified by:
apply
in interfaceorg.junit.rules.TestRule
-
inject
public void inject()Completes Dagger injection. Must be called before accessing inject types. Must be called after any non-static test module have been added. IfdelayComponentReady()
was used, this must be called aftercomponentReady()
. -
delayComponentReady
Delays creating the component untilcomponentReady()
is called. This is only necessary in the case that a dynamically bound value (e.g. configuring an @BindValue field in @Before or @Test method) is requested before test case execution begins.Examples of early binding requests include an Activity launched by a test rule, or an entry points in a
OnComponentReadyRunner
.If this method is called,
componentReady()
must be called before the test case finishes. -
componentReady
Completes Dagger component creation ifdelayComponentReady()
was used. Binds the current value ofBindValue
fields. Normally this happens automatically. This method may only be called ifdelayComponentReady()
was used to delay value binding.- Returns:
- an instance of the test rule for chaining
-