I'm trying to use Hilt for dependency injection but it gives the error java.lang.IllegalStateException: The component was not created. Check that you have added the HiltAndroidRule.
The HiltAndroidRule is added though:
@RunWith(AndroidJUnit4.class)
@UninstallModules(ItemsModule.class)
@HiltAndroidTest
public class SelectItemActivityTest {
@Rule
public HiltAndroidRule hiltRule = new HiltAndroidRule(this);
@Before
public void init() {
hiltRule.inject();
}
@BindValue
List<Item> items = getItems();
List<Item> getItems() {
List<Item> items = new ArrayList<>();
items.add(new Item(1, "Item1", "", true, true, true));;
items.add(new Item(2, "Item2", "", true, true, true));;
items.add(new Item(3, "Item3", "", true, true, true));;
return items;
}
@Rule
public ActivityTestRule<SelectItemActivity> mActivityRule =
new ActivityTestRule<>(SelectItemActivity.class);
@Test
public void text_isDisplayed() {
onView(withText("Item1")).check(matches(isDisplayed()));
}
}
I've also tried adding an ItemsModule inside the class but that had the same result.
You must wrap it using RuleChain
or by applying order
parameter to the Rule
Annotation.
It is explained in detail here: https://developer.android.com/training/dependency-injection/hilt-testing#multiple-testrules
I was having the same error when I was trying to test an activity that was not the launcher one. I am using Kotlin, but something very similar should apply to Java.
Let's say you want to test MyActivity
. First you need to define your ActivityTestRule
as a not launch activity:
val targetContext: Context = InstrumentationRegistry.getInstrumentation().targetContext
@get:Rule(order = 0)
var hiltRule = HiltAndroidRule(this)
@get:Rule(order = 1)
var testRule = ActivityTestRule(MyActivity::class.java, false, false)
And then, launch your activity, after hilt injection:
@Before fun setup() {
hiltRule.inject()
testRule.launchActivity(Intent(targetContext, MyActivity::class.java))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With