junit 使用自定义初始值设定项和Hilt测试WorkManager

nukf8bse  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(110)

我正在尝试为我的自定义工作管理器实现插装测试,该工作管理器使用Hilt的@AssistedInject。
我的工作管理器在某个应用程序中表现完美,但当我尝试根据Google's work manager integration test guide对其进行测试时,却出现错误:
WM-工人工厂:java.lang.NoSuchMethodException:[类android.内容.上下文,类androidx.工作.工作参数]
我已经关闭了AndroidManifest.xml中的默认初始化:

<provider
        android:name="androidx.startup.InitializationProvider"
        android:authorities="${applicationId}.androidx-startup"
        tools:node="remove">
    </provider>

已实现的配置。我的应用程序类中的提供程序:

@HiltAndroidApp
    class MyApplication : Application(), Configuration.Provider {
        @Inject
        lateinit var workerFactory: HiltWorkerFactory
        override fun getWorkManagerConfiguration(): Configuration {
            return Configuration.Builder().setWorkerFactory(workerFactory)
                .setMinimumLoggingLevel(android.util.Log.DEBUG).build()
        }
    }

设置我的Worker类:

@HiltWorker
class SyncWorker @AssistedInject constructor(
    @Assisted applicationContext: Context,
    @Assisted workerParams: WorkerParameters,
    private val someRepository: SomeRepository,
    private val dispatchers: Dispatchers,
) : CoroutineWorker(applicationContext, workerParams) {
    override suspend fun doWork(): Result {
        val result = withContext(dispatchers.IO) {
            someRepository.synchronize()
        }
        return if (result) Result.success() else Result.retry()
    }
}

我的库的测试构建.gradle配置:

// Test
testImplementation 'junit:junit:4.+'
androidTestImplementation "androidx.work:work-testing:2.7.1"
androidTestImplementation "com.google.dagger:hilt-android-testing:2.43"
kaptAndroidTest "com.google.dagger:hilt-android-compiler:2.43"
kaptAndroidTest 'com.google.dagger:hilt-compiler:2.43'
kaptAndroidTest 'androidx.hilt:hilt-compiler:1.0.0'
androidTestImplementation "androidx.test:runner:1.4.0"
androidTestImplementation "androidx.test:rules:1.4.0"
androidTestImplementation 'androidx.hilt:hilt-work:1.0.0'
implementation 'androidx.test.ext:junit-ktx:1.1.3'

我的 Jmeter 化测试:

@HiltAndroidTest
class SyncWorkerTest {

    @get:Rule
    val hiltRule = HiltAndroidRule(this)
    private val context: Context = InstrumentationRegistry.getInstrumentation().targetContext

    @Before
    fun setUp() {
        val config = Configuration.Builder()
            .setMinimumLoggingLevel(Log.DEBUG)
            .setExecutor(SynchronousExecutor())
            .build()
        WorkManagerTestInitHelper.initializeTestWorkManager(context, config)
    }

    @Test
    fun testDoWork() {
        val request = SyncWorker.startSyncJob()

        val workManager = WorkManager.getInstance(context)
        val testDriver = WorkManagerTestInitHelper.getTestDriver(context)!!

        workManager.enqueueUniqueWork(
            SyncWorker.SyncWorkName,
            ExistingWorkPolicy.REPLACE,
            request,
        )

        val preRunWorkInfo = workManager.getWorkInfoById(request.id).get()

        Assert.assertEquals(WorkInfo.State.ENQUEUED, preRunWorkInfo.state)

        testDriver.setAllConstraintsMet(request.id)
    }
}

实现了我自己的JUnitRunner,并在我的模块的build.gradle中指定了它:

class YALTestRunner : AndroidJUnitRunner() {
        override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application {
            return super.newApplication(cl, HiltTestApplication::class.java.name, context)
        }
    }

testInstrumentationRunner "com.android.wmapp.YALTestRunner"
我在工作管理器的测试实现中遗漏了什么?

yqhsw0fo

yqhsw0fo1#

我认为清单中的配置不正确。请检查此处。对于2.6版及更高版本,配置应该有所不同:
https://developer.android.com/topic/libraries/architecture/workmanager/advanced/custom-configuration#remove-default

相关问题