我正在尝试使用 Hilt 进行依赖注入,但它给出了错误java.lang.IllegalStateException: The component was not created. Check that you have added the HiltAndroidRule.
The HiltAndroidRule is added 虽然:
@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()));
}
}
我也尝试在类中添加一个 ItemsModule ,但结果相同。