android - 使用 Workmanager 进行 Hilt Instrumentation 测试不工作

标签 android android-espresso android-workmanager android-instrumentation dagger-hilt

当我尝试在包含 WorkManager 的应用程序中运行 ActivityScenario 时,我在启动时收到以下错误:

java.lang.IllegalStateException: WorkManager is not initialized properly.  You have explicitly disabled WorkManagerInitializer in your manifest, have not manually called WorkManager#initialize at this point, and your Application does not implement Configuration.Provider.
使用 WorkManagerTestInitHelper来自 work-test神器也无济于事。
WorkManager 的定义如下:
@Provides
@Singleton
fun provideWorkmanager(@ApplicationContext context: Context) = WorkManager.getInstance(context)
这是我的测试自动取款机:
    @HiltAndroidTest
    @RunWith(AndroidJUnit4::class)
    class LoginTest {
    
        @get:Rule(order = 0)
        var hiltRule = HiltAndroidRule(this)
    
        @get:Rule(order = 1)
        val activityRule = ActivityScenarioRule(MainActivity::class.java)
    
        @Before
        fun before() {
            val context = InstrumentationRegistry.getInstrumentation().targetContext
            val config = Configuration.Builder()
                .setMinimumLoggingLevel(Log.DEBUG)
                .setExecutor(SynchronousExecutor())
                .build()
            WorkManagerTestInitHelper.initializeTestWorkManager(context, config)
        }
    
        @Test
        fun test() {
            ...
        }

}

最佳答案

这是因为 @get:Rule@Before 之前执行确实,根据 Google Documentation :

This rule provides functional testing of a single activity. The activity under test is launched before each test annotated with @Test and before any method annotated with @Before. It's terminated after the test is completed and all methods annotated with @After are finished. To access the activity under test in your test logic, provide a callback runnable to ActivityScenarioRule.getScenario().onActivity().


为了解决这个问题,您需要初始化 WorkManager在与 WorkManagerTestInitHelper 的测试中之前 您尝试启动 Activity 。
为此,您应该避免使用 ActivityScenarioRule并使用 ActivityScenario相反,您可以执行以下操作:
@HiltAndroidTest
@RunWith(AndroidJUnit4::class)
class LoginTest {
    private lateinit var scenario: ActivityScenario<MainActivity>

    @get:Rule
    var hiltRule = HiltAndroidRule(this)

    @Before
    fun before() {
        val context = InstrumentationRegistry.getInstrumentation().targetContext
        val config = Configuration.Builder()
            .setMinimumLoggingLevel(Log.DEBUG)
            .setExecutor(SynchronousExecutor())
            .build()
        WorkManagerTestInitHelper.initializeTestWorkManager(context, config)

        scenario = launchActivity()
    }

    @Test
    fun test() {
        scenario.moveToState(Lifecycle.State.CREATED).onActivity { 
             activity -> // do some test with the activity
        }
    }
}

关于android - 使用 Workmanager 进行 Hilt Instrumentation 测试不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63671323/

相关文章:

android - 如何在 Android 中检测触摸事件

android-espresso - Espresso UI 测试框架是否支持 Webviews?

android - 为具有泛型的类创建 Espresso 匹配器

android - 如何使用 setRequiredNetworkType(NetworkType.CONNECTED) 约束测试 Android WorkManager?

Android 周期性工作对打瞌睡模式和应用待机友好

android - 如何对 WorkManager Worker 进行单元测试

java - Android 异步类返回 null

android - 将 v21 中的 AppCompatButton 设置为没有阴影和角半径为零

android - Kotlin 和新的 ActivityTestRule : The @Rule must be public

java - Android 和 Arduino 之间的蓝牙数据丢失