【发布时间】:2020-12-09 07:38:16
【问题描述】:
我正在尝试为 Android 应用运行插桩测试,但出现此编译时错误:
Cannot mix test roots with non-test roots:
Non-Test Roots: com.my.application.MyApplication
Test Roots: [com.my.test.ActivityTest]
该应用程序使用带有 @HiltAndroidApp 注释的自定义应用程序,因为文档暗示这是 Hilt 所必需的。
我的测试遵循这个模式:
@RunWith(AndroidJUnit4.class)
@HiltAndroidTest
class MyTest {
@Rule
public HiltAndroidRule hiltRule = new HiltAndroidRule(this);
@Inject
SomeStuff stuff;
@Before
public void setUp() {
hiltRule.inject();
}
应用运行器正在创建一个普通的 Hilt 应用:
public class MyAppRunner extends AndroidJUnitRunner {
@Override
public Application newApplication(ClassLoader cl, String className, Context context)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
return super.newApplication(
cl, HiltTestApplication.class.getName(), context);
运行测试时,我收到上述编译时错误。我想MyApplication 的引用来自应用程序的AndroidManifest.xml 文件。
我尝试将@CustomTestApplication 与MyApplication 一起使用,但自定义测试应用程序显然不适用于带有@HiltAndroidApp 注释的应用程序。
使用基于 Hilt 的应用程序设置测试的正确方法是什么?
【问题讨论】:
标签: android dagger-hilt