为什么在使用 Robolectric 的 Android 单元测试中调用 HtmlCompat 时会出现 ExceptionInInitializerError?
Why does ExceptionInInitializerError occur when invoking HtmlCompat in an Android unit test using Robolectric?
我正在尝试对属于 Android 应用程序用例层的方法进行单元测试。该方法接收 XML RSS 提要并将其作为 GSON 解析对象 returns 发送到视图模型。测试 class 用 @RunWith(RobolectricTestRunner::class)
注释。
测试失败,因为在用例 class:
的这个方法中 .fromHtml()
抛出了 java.lang.ExceptionInInitializerError
(以及其他)
private fun convertHtml(rawString: String): String {
return HtmlCompat
.fromHtml(rawString, HtmlCompat.FROM_HTML_MODE_COMPACT)
.toString()
.replace("", "")
.trim()
}
在读取堆栈跟踪时,这似乎是 Robolectric 尝试初始化静态方法或 class 的问题?我这样说是因为堆栈跟踪中的一些行,例如:
at org.robolectric.internal.bytecode.RobolectricInternals.performStaticInitialization(RobolectricInternals.java:61)
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:178)
不幸的是,我所有的研究都无济于事。我似乎找不到与此问题相关的任何内容。我已经看到几个线程讨论一起使用 Robolectric 和 PowerMock 来测试静态 classes,尽管我不确定这是否是相关方法,并且我想尽可能避免添加更多测试框架. 为什么会抛出这个异常,如何解决这个异常?
作为参考,这里是整个堆栈跟踪:
java.lang.ExceptionInInitializerError
at android.text.SpannableStringBuilder.__constructor__(SpannableStringBuilder.java:61)
at android.text.SpannableStringBuilder.<init>(SpannableStringBuilder.java)
at android.text.HtmlToSpannedConverter.__constructor__(Html.java:425)
at android.text.HtmlToSpannedConverter.<init>(Html.java)
at android.text.Html.fromHtml(Html.java:135)
at android.text.Html.fromHtml(Html.java:101)
at androidx.core.text.HtmlCompat.fromHtml(HtmlCompat.java:150)
at com.domg.testrss.domain.usecase.GetNewsHeadlinesUseCase.convertHtml(GetNewsHeadlinesUseCase.kt:162)
at com.domg.testrss.domain.usecase.GetNewsHeadlinesUseCase.convertXmlToJson(GetNewsHeadlinesUseCase.kt:80)
at com.domg.testrss.domain.usecase.GetNewsHeadlinesUseCase.execute(GetNewsHeadlinesUseCase.kt:35)
at com.domg.testrss.GetNewsHeadlinesUseCaseTest$parsing success.invokeSuspend(GetNewsHeadlinesUseCaseTest.kt:33)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:279)
at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
at com.domg.testrss.GetNewsHeadlinesUseCaseTest.parsing success(GetNewsHeadlinesUseCaseTest.kt:32)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:306)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.evaluate(RobolectricTestRunner.java:570)
at org.robolectric.internal.SandboxTestRunner.lambda$evaluate[=14=](SandboxTestRunner.java:278)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread[=14=](Sandbox.java:89)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:181)
at org.robolectric.internal.bytecode.RobolectricInternals.classInitializing(RobolectricInternals.java:21)
at android.text.TextUtils.<clinit>(TextUtils.java)
at android.text.SpannableStringBuilder.$$robo$$android_text_SpannableStringBuilder$__constructor__(SpannableStringBuilder.java:61)
at android.text.SpannableStringBuilder.<init>(SpannableStringBuilder.java)
at android.text.SpannableStringBuilder.<init>(SpannableStringBuilder.java)
at android.text.SpannableStringBuilder.<init>(SpannableStringBuilder.java)
at android.text.HtmlToSpannedConverter.$$robo$$android_text_HtmlToSpannedConverter$__constructor__(Html.java:425)
at android.text.HtmlToSpannedConverter.<init>(Html.java)
at android.text.Html.$$robo$$android_text_Html$fromHtml(Html.java:135)
at android.text.Html.fromHtml(Html.java)
at android.text.Html.$$robo$$android_text_Html$fromHtml(Html.java:101)
at android.text.Html.fromHtml(Html.java)
at androidx.core.text.HtmlCompat.fromHtml(HtmlCompat.java:150)
at com.domg.testrss.domain.usecase.GetNewsHeadlinesUseCase.convertHtml(GetNewsHeadlinesUseCase.kt:162)
at com.domg.testrss.domain.usecase.GetNewsHeadlinesUseCase.convertXmlToJson(GetNewsHeadlinesUseCase.kt:80)
at com.domg.testrss.domain.usecase.GetNewsHeadlinesUseCase.execute(GetNewsHeadlinesUseCase.kt:35)
at com.domg.testrss.GetNewsHeadlinesUseCaseTest$parsing success.invokeSuspend(GetNewsHeadlinesUseCaseTest.kt:33)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:279)
at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
at com.domg.testrss.GetNewsHeadlinesUseCaseTest.parsing success(GetNewsHeadlinesUseCaseTest.kt:32)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
... 13 more
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.robolectric.internal.bytecode.RobolectricInternals.performStaticInitialization(RobolectricInternals.java:61)
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:178)
... 43 more
Caused by: java.lang.NullPointerException
at org.robolectric.shadows.ShadowLegacyAssetManager.getAndResolve(ShadowLegacyAssetManager.java:1019)
at org.robolectric.shadows.ShadowLegacyAssetManager.getResourceText(ShadowLegacyAssetManager.java:285)
at android.content.res.AssetManager.getResourceText(AssetManager.java)
at android.content.res.Resources.getText(Resources.java:225)
at android.content.res.Resources.getString(Resources.java:313)
at android.text.TextUtils.__staticInitializer__(TextUtils.java:1704)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.robolectric.internal.bytecode.RobolectricInternals.performStaticInitialization(RobolectricInternals.java:61)
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:178)
at org.robolectric.internal.bytecode.RobolectricInternals.classInitializing(RobolectricInternals.java:21)
at android.text.TextUtils.<clinit>(TextUtils.java)
at android.text.SpannableStringBuilder.__constructor__(SpannableStringBuilder.java:61)
at android.text.SpannableStringBuilder.<init>(SpannableStringBuilder.java)
at android.text.HtmlToSpannedConverter.__constructor__(Html.java:425)
at android.text.HtmlToSpannedConverter.<init>(Html.java)
at android.text.Html.fromHtml(Html.java:135)
at android.text.Html.fromHtml(Html.java:101)
at androidx.core.text.HtmlCompat.fromHtml(HtmlCompat.java:150)
at com.domg.testrss.domain.usecase.GetNewsHeadlinesUseCase.convertHtml(GetNewsHeadlinesUseCase.kt:162)
at com.domg.testrss.domain.usecase.GetNewsHeadlinesUseCase.convertXmlToJson(GetNewsHeadlinesUseCase.kt:80)
at com.domg.testrss.domain.usecase.GetNewsHeadlinesUseCase.execute(GetNewsHeadlinesUseCase.kt:35)
at com.domg.testrss.GetNewsHeadlinesUseCaseTest$parsing success.invokeSuspend(GetNewsHeadlinesUseCaseTest.kt:33)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:279)
at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
at com.domg.testrss.GetNewsHeadlinesUseCaseTest.parsing success(GetNewsHeadlinesUseCaseTest.kt:32)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
... 13 more
我找到了解决办法。将以下内容添加到 模块 build.gradle
的 android
部分:
testOptions {
unitTests {
includeAndroidResources = true
}
}
根据 documentation,将此设置为 true 将允许单元测试通过“执行[ing]资源、资产和清单合并”来“使用 Android 资源、资产和清单”在 运行 你的单元测试之前。"
按上述方式添加后,异常停止,我的单元测试调用 HtmlCompat
能够完成。
我正在尝试对属于 Android 应用程序用例层的方法进行单元测试。该方法接收 XML RSS 提要并将其作为 GSON 解析对象 returns 发送到视图模型。测试 class 用 @RunWith(RobolectricTestRunner::class)
注释。
测试失败,因为在用例 class:
的这个方法中.fromHtml()
抛出了 java.lang.ExceptionInInitializerError
(以及其他)
private fun convertHtml(rawString: String): String {
return HtmlCompat
.fromHtml(rawString, HtmlCompat.FROM_HTML_MODE_COMPACT)
.toString()
.replace("", "")
.trim()
}
在读取堆栈跟踪时,这似乎是 Robolectric 尝试初始化静态方法或 class 的问题?我这样说是因为堆栈跟踪中的一些行,例如:
at org.robolectric.internal.bytecode.RobolectricInternals.performStaticInitialization(RobolectricInternals.java:61)
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:178)
不幸的是,我所有的研究都无济于事。我似乎找不到与此问题相关的任何内容。我已经看到几个线程讨论一起使用 Robolectric 和 PowerMock 来测试静态 classes,尽管我不确定这是否是相关方法,并且我想尽可能避免添加更多测试框架. 为什么会抛出这个异常,如何解决这个异常?
作为参考,这里是整个堆栈跟踪:
java.lang.ExceptionInInitializerError
at android.text.SpannableStringBuilder.__constructor__(SpannableStringBuilder.java:61)
at android.text.SpannableStringBuilder.<init>(SpannableStringBuilder.java)
at android.text.HtmlToSpannedConverter.__constructor__(Html.java:425)
at android.text.HtmlToSpannedConverter.<init>(Html.java)
at android.text.Html.fromHtml(Html.java:135)
at android.text.Html.fromHtml(Html.java:101)
at androidx.core.text.HtmlCompat.fromHtml(HtmlCompat.java:150)
at com.domg.testrss.domain.usecase.GetNewsHeadlinesUseCase.convertHtml(GetNewsHeadlinesUseCase.kt:162)
at com.domg.testrss.domain.usecase.GetNewsHeadlinesUseCase.convertXmlToJson(GetNewsHeadlinesUseCase.kt:80)
at com.domg.testrss.domain.usecase.GetNewsHeadlinesUseCase.execute(GetNewsHeadlinesUseCase.kt:35)
at com.domg.testrss.GetNewsHeadlinesUseCaseTest$parsing success.invokeSuspend(GetNewsHeadlinesUseCaseTest.kt:33)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:279)
at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
at com.domg.testrss.GetNewsHeadlinesUseCaseTest.parsing success(GetNewsHeadlinesUseCaseTest.kt:32)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:306)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.evaluate(RobolectricTestRunner.java:570)
at org.robolectric.internal.SandboxTestRunner.lambda$evaluate[=14=](SandboxTestRunner.java:278)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread[=14=](Sandbox.java:89)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:181)
at org.robolectric.internal.bytecode.RobolectricInternals.classInitializing(RobolectricInternals.java:21)
at android.text.TextUtils.<clinit>(TextUtils.java)
at android.text.SpannableStringBuilder.$$robo$$android_text_SpannableStringBuilder$__constructor__(SpannableStringBuilder.java:61)
at android.text.SpannableStringBuilder.<init>(SpannableStringBuilder.java)
at android.text.SpannableStringBuilder.<init>(SpannableStringBuilder.java)
at android.text.SpannableStringBuilder.<init>(SpannableStringBuilder.java)
at android.text.HtmlToSpannedConverter.$$robo$$android_text_HtmlToSpannedConverter$__constructor__(Html.java:425)
at android.text.HtmlToSpannedConverter.<init>(Html.java)
at android.text.Html.$$robo$$android_text_Html$fromHtml(Html.java:135)
at android.text.Html.fromHtml(Html.java)
at android.text.Html.$$robo$$android_text_Html$fromHtml(Html.java:101)
at android.text.Html.fromHtml(Html.java)
at androidx.core.text.HtmlCompat.fromHtml(HtmlCompat.java:150)
at com.domg.testrss.domain.usecase.GetNewsHeadlinesUseCase.convertHtml(GetNewsHeadlinesUseCase.kt:162)
at com.domg.testrss.domain.usecase.GetNewsHeadlinesUseCase.convertXmlToJson(GetNewsHeadlinesUseCase.kt:80)
at com.domg.testrss.domain.usecase.GetNewsHeadlinesUseCase.execute(GetNewsHeadlinesUseCase.kt:35)
at com.domg.testrss.GetNewsHeadlinesUseCaseTest$parsing success.invokeSuspend(GetNewsHeadlinesUseCaseTest.kt:33)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:279)
at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
at com.domg.testrss.GetNewsHeadlinesUseCaseTest.parsing success(GetNewsHeadlinesUseCaseTest.kt:32)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
... 13 more
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.robolectric.internal.bytecode.RobolectricInternals.performStaticInitialization(RobolectricInternals.java:61)
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:178)
... 43 more
Caused by: java.lang.NullPointerException
at org.robolectric.shadows.ShadowLegacyAssetManager.getAndResolve(ShadowLegacyAssetManager.java:1019)
at org.robolectric.shadows.ShadowLegacyAssetManager.getResourceText(ShadowLegacyAssetManager.java:285)
at android.content.res.AssetManager.getResourceText(AssetManager.java)
at android.content.res.Resources.getText(Resources.java:225)
at android.content.res.Resources.getString(Resources.java:313)
at android.text.TextUtils.__staticInitializer__(TextUtils.java:1704)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.robolectric.internal.bytecode.RobolectricInternals.performStaticInitialization(RobolectricInternals.java:61)
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:178)
at org.robolectric.internal.bytecode.RobolectricInternals.classInitializing(RobolectricInternals.java:21)
at android.text.TextUtils.<clinit>(TextUtils.java)
at android.text.SpannableStringBuilder.__constructor__(SpannableStringBuilder.java:61)
at android.text.SpannableStringBuilder.<init>(SpannableStringBuilder.java)
at android.text.HtmlToSpannedConverter.__constructor__(Html.java:425)
at android.text.HtmlToSpannedConverter.<init>(Html.java)
at android.text.Html.fromHtml(Html.java:135)
at android.text.Html.fromHtml(Html.java:101)
at androidx.core.text.HtmlCompat.fromHtml(HtmlCompat.java:150)
at com.domg.testrss.domain.usecase.GetNewsHeadlinesUseCase.convertHtml(GetNewsHeadlinesUseCase.kt:162)
at com.domg.testrss.domain.usecase.GetNewsHeadlinesUseCase.convertXmlToJson(GetNewsHeadlinesUseCase.kt:80)
at com.domg.testrss.domain.usecase.GetNewsHeadlinesUseCase.execute(GetNewsHeadlinesUseCase.kt:35)
at com.domg.testrss.GetNewsHeadlinesUseCaseTest$parsing success.invokeSuspend(GetNewsHeadlinesUseCaseTest.kt:33)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:279)
at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
at com.domg.testrss.GetNewsHeadlinesUseCaseTest.parsing success(GetNewsHeadlinesUseCaseTest.kt:32)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
... 13 more
我找到了解决办法。将以下内容添加到 模块 build.gradle
的 android
部分:
testOptions {
unitTests {
includeAndroidResources = true
}
}
根据 documentation,将此设置为 true 将允许单元测试通过“执行[ing]资源、资产和清单合并”来“使用 Android 资源、资产和清单”在 运行 你的单元测试之前。"
按上述方式添加后,异常停止,我的单元测试调用 HtmlCompat
能够完成。