单元测试框架之Robolectric踩坑

本人在刚开始学习这个框架的时候遇到了一些坑,通过各种网上查找资料后,最终解决了一些问题,记录一下:

java.lang.VerifyError: Expecting a stackmap frame at branch target 9

java.lang.VerifyError: Expecting a stackmap frame at branch target 9
Exception Details:
  Location:
    com/meizu/cloud/pushsdk/MzPushMessageReceiver.onReceive(Landroid/content/Context;Landroid/content/Intent;)V @0: aload_0
  Reason:
    Expected stackmap frame at this location.
  Bytecode:
    0x0000000: 2a2b 2cb7 0019 a700 444e 1203 bb00 0f59
    0x0000010: b700 1c12 02b6 001d 2db6 001b b600 1db6
    0x0000020: 001e b800 122b 2bb6 0010 0101 1201 bb00
    0x0000030: 0f59 b700 1c12 04b6 001d 2db6 001b b600
    0x0000040: 1db6 001e 110b b8b8 001a b1            
  Exception Handler Table:
    bci [0, 6] => handler: 9


	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)
	at org.robolectric.internal.Shadow.newInstanceOf(Shadow.java:15)
	at org.robolectric.shadows.ShadowApplication.registerBroadcastReceivers(ShadowApplication.java:148)
	at org.robolectric.shadows.ShadowApplication.bind(ShadowApplication.java:137)
	at org.robolectric.shadows.CoreShadowsAdapter.bind(CoreShadowsAdapter.java:99)
	at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:121)
	at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
	at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
	at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
	at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
复制代码

这是jvm的编译问题,我们需要设置-noverify参数,在AndroidStudio中,Go to Run -> Edit Configurations -> Configuration Tab

android.content.res.Resources$NotFoundException: unknown resource 2130837589

at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:351)
	at org.robolectric.shadows.ShadowAssetManager.getResourceValue(ShadowAssetManager.java:102)
	at android.content.res.AssetManager.getResourceValue(AssetManager.java)
	at android.content.res.Resources.getValue(Resources.java:1229)
	at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:328)
	at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:193)
	at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
	at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:753)
	at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
	at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:85)
	at android.support.v7.app.AppCompatDelegateImplBase.__constructor__(AppCompatDelegateImplBase.java:128)
	at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java)
	at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java)
	at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java)
	at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:202)
	at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
	at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
	at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:70)
	at com.sunland.core.ui.base.BaseActivity.onCreate(BaseActivity.java:46)
	at com.sunland.ehr.attendance.clockin.ClockInActivity.onCreate(ClockInActivity.java:204)
	at android.app.Activity.performCreate(Activity.java:5933)
	at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:195)
	at org.robolectric.util.ActivityController$1.run(ActivityController.java:122)
	at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:304)
	at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:45)
	at org.robolectric.util.ActivityController.create(ActivityController.java:118)
	at org.robolectric.util.ActivityController.create(ActivityController.java:129)
	at org.robolectric.util.ActivityController.setup(ActivityController.java:210)
	at org.robolectric.Robolectric.setupActivity(Robolectric.java:46)
	at ClockInActivityTest.setUp(ClockInActivityTest.java:28)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
	at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:251)
	at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
	at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
	at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
复制代码

这个问题坑比较大,在网上找到的各种解决办法都没有生效,反正是资源文件和AndroidManifest文件找不到引起的,之前以为由于是组件化引起的资源找不到的问题,事实上不是这样的。我们在test类里面引用的是RobolectricTestRunner,例如@RunWith(RobolectricTestRunner.class),实际上有个专门用于编译资源的类,RobolectricGradleTestRunner,该类的说明

/**
 * Test runner customized for running unit tests either through the Gradle CLI or
 * Android Studio. The runner uses the build type and build flavor to compute the
 * resource, asset, and AndroidManifest paths.
 *
 * This test runner requires that you set the 'constants' field on the @Config
 * annotation (or the org.robolectric.Config.properties file) for your tests.
 */
public class RobolectricGradleTestRunner extends RobolectricTestRunner {
  private static final String BUILD_OUTPUT = "build/intermediates";

  public RobolectricGradleTestRunner(Class<?> klass) throws InitializationError {
    super(klass);
复制代码

我们只要在Test类上替换一下注解就可以解决这个问题;

Caused by: java.lang.RuntimeException: MultiDex installation failed (/Users/wangchao/code/staffapp/app/. (Is a directory)).

Caused by: java.lang.RuntimeException: MultiDex installation failed (/Users/wangchao/code/staffapp/app/. (Is a directory)).
	at android.support.multidex.MultiDex.install(MultiDex.java:121)
	at com.sunland.core.ui.base.BaseApplication.attachBaseContext(BaseApplication.java:70)
	at android.app.Application.attach(Application.java:181)
	at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:195)
	at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:131)
	at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
	at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
	at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
	at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
	... 1 more
复制代码
这里只要添加 multi-dex shadow dependency:
复制代码
testImplementation "org.robolectric:multidex:3.4.2"
复制代码

Robolectric > 3.4 使用

testImplementation "org.robolectric:shadows-multidex:4.0.1"
复制代码

转载于:https://juejin.im/post/5c7515c56fb9a049f362c0f2

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值