Class HiltAndroidRule

java.lang.Object
dagger.hilt.android.testing.HiltAndroidRule
All Implemented Interfaces:
org.junit.rules.TestRule

public final class HiltAndroidRule extends Object implements 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 Details

    • HiltAndroidRule

      public HiltAndroidRule(Object testInstance)
      Creates a new instance of the rules. Tests should pass this.
  • 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 interface org.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. If delayComponentReady() was used, this must be called after componentReady().
    • delayComponentReady

      public HiltAndroidRule delayComponentReady()
      Delays creating the component until componentReady() 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

      public HiltAndroidRule componentReady()
      Completes Dagger component creation if delayComponentReady() was used. Binds the current value of BindValue fields. Normally this happens automatically. This method may only be called if delayComponentReady() was used to delay value binding.
      Returns:
      an instance of the test rule for chaining