当我尝试开始我的一项junit
测试时,由于多个错误,所有测试都失败了。这是因为 myApplication class
中有一些变量,这些变量在“正常”应用程序启动时初始化,但不是在我开始测试时初始化。注释掉变量可能会有所帮助,但这是一个丑陋的解决方案,在开始测试时总是注释掉一些东西。
应用类
@HiltAndroidApp
@InternalCoroutinesApi
class App : Application(), Configuration.Provider {
@Inject lateinit var workerFactory: HiltWorkerFactory
@Inject lateinit var workerList: WorkerList
override fun onCreate() {
super.onCreate()
plantTimberTree()
enableOfflinePersistence()
workerList.startDownloadDocumentWork()
}
override fun getWorkManagerConfiguration(): Configuration =
Configuration.Builder().setWorkerFactory(workerFactory).build()
}
private fun plantTimberTree() = Timber.plant(Timber.DebugTree())
private fun enableOfflinePersistence() {
val settings = firestoreSettings { isPersistenceEnabled = true }
Firebase.firestore.firestoreSettings = settings
}
测试用例
@RunWith(RobolectricTestRunner::class)
@Config(maxSdk = Build.VERSION_CODES.P, minSdk = Build.VERSION_CODES.P)
class EmailViewModelTest {
}
错误堆栈跟踪(没有任何评论)
java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process null. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(FirebaseApp.java:184)
at com.google.firebase.firestore.FirebaseFirestore.getInstance(FirebaseFirestore.java:91)
at com.google.firebase.firestore.ktx.FirestoreKt.getFirestore(Firestore.kt:32)
at com.example.app.AppKt.enableOfflinePersistence(App.kt:44)
at com.example.app.AppKt.access$enableOfflinePersistence(App.kt:1)
at com.example.app.App.onCreate(App.kt:24)
at org.robolectric.android.internal.AndroidTestEnvironment.lambda$installAndCreateApplication$0(AndroidTestEnvironment.java:288)
at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:75)
at org.robolectric.android.internal.AndroidTestEnvironment.installAndCreateApplication(AndroidTestEnvironment.java:288)
at org.robolectric.android.internal.AndroidTestEnvironment.setUpApplicationState(AndroidTestEnvironment.java:171)
at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:319)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:254)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
错误堆栈跟踪(注释掉“enableOfflicePersistence”时)
kotlin.UninitializedPropertyAccessException: lateinit property workerList has not been initialized
at com.example.app.App.onCreate(App.kt:25)
at org.robolectric.android.internal.AndroidTestEnvironment.lambda$installAndCreateApplication$0(AndroidTestEnvironment.java:288)
at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:75)
at org.robolectric.android.internal.AndroidTestEnvironment.installAndCreateApplication(AndroidTestEnvironment.java:288)
at org.robolectric.android.internal.AndroidTestEnvironment.setUpApplicationState(AndroidTestEnvironment.java:171)
at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:319)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:254)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
有没有办法确定测试是否已经开始?我应该如何更改我的应用程序类才能让我的测试再次运行?
我感谢每一个帮助,谢谢。