使用Robolectric 4.1的ShadowOf()API时出现运行时错误

问题描述 投票:0回答:2

我正在尝试使用Robolectric的ShadowOf()API来验证在开发代码中调用适配器的notifyDatasetChanged时listview中的项目是否显示为具有更改的数据值。

为此,我正在尝试获取ShadowListView并进行验证:

ListView lv = myFragment.getView().findViewById(R.id.myActualListView);
ShadowListView shadowListView = Shadows.shadowOf(lv);  // <-- fails at runtime
shadowListView.populateItems();

assertTrue(shadowOf(adapter).wasNotifyDataSetChangedCalled());      
assertTrue(lv.getChildCount() > 0);  

//more lines of code to access list item

然而,在运行测试类时,我却不断为不相关的类产生错误。

较早,在获取错误:

错误:无法访问EuiccManager类文件找不到android.telephony.euicc.EuiccManager

在build.gradle文件中的以下行之后:

testImplementation 'org.robolectric:robolectric:3.6.1' 

获取SliceManager的错误,我在代码中的任何地方都没有使用过-

错误:无法访问SliceManager类文件找不到android.app.slice.SliceManager

下面的我的gradle文件:

    //Robolectric
  testImplementation 'org.robolectric:robolectric:4.1'
   // testImplementation ('org.robolectric:shadows-supportv4:4.1')

   testImplementation 'org.robolectric:robolectric:3.6.1'


    testImplementation 'androidx.test:core:1.1.0'
    testImplementation 'org.mockito:mockito-core:2.8.9'
    androidTestImplementation 'org.mockito:mockito-android:2.8.9'
    testImplementation 'com.google.truth:truth:0.42'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
    androidTestImplementation 'junit:junit:4.12'
    testImplementation 'com.google.code.findbugs:jsr305:1.3.9'
    testImplementation 'org.powermock:powermock-api-mockito2:1.7.1'
    testImplementation 'org.powermock:powermock-module-junit4:1.7.1'

由于缺少类而出现此错误,因此我无法使用Shadows.shadowOf()API。是否有任何方法可以对列表视图相对于在开发代码中调用的notifydatasetchange()在屏幕上显示修改后的数据值进行单元测试?

robolectric
2个回答
1
投票

添加Robolectric的4.2版本。所有问题均在4.2版本中解决。

    *testImplementation 'org.robolectric:robolectric:4.2'*

注意:Android SDK版本28


0
投票

将compileSdkVersion升级到API 28。

发生这种情况是因为新版本的robolectric在shadowOf函数中使用了android.net.wifi.rtt.WifiRttManager,并且API 28中引入了它,因此必须对其进行升级。

© www.soinside.com 2019 - 2024. All rights reserved.