使用ShinobiControls和Robolectric验证VerifyError

时间:2016-01-27 04:19:33

标签: android unit-testing robolectric shinobi robolectric-gradle-plugin

我在尝试运行Robolectric测试时遇到了这个奇怪的VerifyError,这些测试调用了ShinobiControls。我已经尝试了-noverify选项,但它似乎无法正常工作,或者我将它放在错误的位置。

我使用的是Retrolambda,Java 8,Robolectric(3.0)和Shinobi Premium(1.8.0)。

java.lang.VerifyError: Expecting a stackmap frame at branch target 18
Exception Details:
  Location:
    com/shinobicontrols/charts/ChartFragment.onResume()V @8: ifnull
  Reason:
    Expected stackmap frame at this location.
  Bytecode:
    0x0000000: 2ab7 000b 2ab4 0006 c600 0a2a b400 06b6
    0x0000010: 0012 b1                                

    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    at java.lang.Class.getConstructor0(Class.java:3075)
    at java.lang.Class.newInstance(Class.java:412)
    at android.app.Fragment.instantiate(Fragment.java:611)
    at android.app.Fragment.instantiate(Fragment.java:582)
    at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2108)
    at android.app.Activity.onCreateView(Activity.java:5282)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
    at android.app.Activity.setContentView(Activity.java:2144)
    at com.example.android.MainActivity.onCreate(MainActivity.java:132)

1 个答案:

答案 0 :(得分:0)

基本上,我们正在解决shinobi观点。我们有下一个建设:

public class ShinobyView {
    @VisibleForTesting
    @NonNull
    protected ChartView getShinobiChart(@NonNull final LayoutInflator inflator, @NonNull final ViewGroup chartContainer) {
        return inflater.inflate(R.layout.shinobi_chart, chartContainer, false);
    }

    @NonNull
    public View createView(@NonNull context context) {
       ...
       ChratView view = getShinobiChart(inflator, container);
       container.addView(view, <some layout params>);
       ...
    }
}

在测试中我们假装它:

public class TestShinobyView {
    @Override
    @NonNull
    protected ChartView 
    getShinobiChart(@NonNull final LayoutInflator inflator, @NonNull final ViewGroup chartContainer) {
        return new View(activity);
    }
}

如果您想验证是否也正确设置了shinobi图表,那么您需要模拟ShinobiChart并且不返回View,而是伪造ShinobiView