Android RecyclerView 适配器在单元测试中给出 null

2024-02-23

我正在尝试使用 AndroidJunit4 测试 RecyclerView,这是我的测试代码:

package com.kaushik.myredmart.ui;
// all includes
@RunWith(AndroidJUnit4.class)
public class ProductListActivityTest {

    @Rule
    public ActivityTestRule<ProductListActivity> rule  = new  ActivityTestRule<>(ProductListActivity.class);

    @Test
    public void ensureListViewIsPresent() throws Exception {
        ProductListActivity activity = rule.getActivity();
        View viewByIdw = activity.findViewById(R.id.productListView);
        assertThat(viewByIdw,notNullValue());
        assertThat(viewByIdw, instanceOf(RecyclerView.class));
        RecyclerView productRecyclerView = (RecyclerView) viewByIdw;
        RecyclerView.Adapter adapter = productRecyclerView.getAdapter();
        assertThat(adapter, instanceOf(ProductAdapter.class));

    }
}

我在检查适配器时遇到问题。尽管productRecyclerView通过了非空测试和RecyclerView实例,但最后一行出现以下错误:

java.lang.AssertionError:
Expected: an instance of com.kaushik.myredmart.adapter.ProductAdapter
but: null
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.junit.Assert.assertThat(Assert.java:956)
at org.junit.Assert.assertThat(Assert.java:923)
at com.kaushik.myredmart.ui.ProductListActivityTest.ensureListViewIsPresent(ProductListActivityTest.java:45)

代码中有什么问题?


从这一行来看:

预期:com.kaushik.myredmart.adapter.ProductAdapter 的实例 但是:空

人们可以得出这样的结论:

RecyclerView.Adapter adapter = productRecyclerView.getAdapter();

returns null,当没有执行时可能会发生productRecyclerView.setAdapter(adapter).

确保您在活动生命周期回调中正确设置适配器(即在onCreate())。在我看来,您正在某些操作/回调后创建和设置适配器。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android RecyclerView 适配器在单元测试中给出 null 的相关文章

随机推荐

  • 如何注册应用程序以在 iPad 中打开我的应用程序中的 pdf 文件

    我想从 pdf 页面在我的应用程序中打开 pdf 文件 但我没有在我的应用程序中打开 pdf 的任何选项 这是我的 info plist 文件
  • 为什么 git稀疏结帐 会导致致命错误?

    这是我的命令git我正在 Windows 中运行 以稀疏存储库中的签出文件夹 git clone verbose filter blob none no checkout depth 1 sparse https user email pr
  • \x1a 字符的含义是什么

    什么是 x1a字符的含义以及为什么mysql real escape string逃避吗 从文档中 mysql real escape string 调用 MySQL 的库函数 mysql real escape string 该函数在以下
  • 获取两个标题之间的差异

    我有这个方法来计算 2 个 0 360 罗盘方向之间的差异 尽管这可以计算出我的绝对偏差 例如 始终为正输出 有多远 但我无法弄清楚需要做什么才能将符号引入到输出中 理想情况下 如果从初始航向到最终航向的最短距离是顺时针旋转 我希望erro
  • 如何在rpart中应用权重?

    我有 Kaggle 实践竞赛中的房屋数据 我正在使用 rpart 训练一个简单的第一个模型来预测销售价格 该模型无法正确识别销售状况异常或预付款的销售 因此 我想增加这个在模型中显然被忽略的变量的重要性 我假设这是通过使用 权重 参数来完成
  • .ssh/config:Mac OS Sierra 10.12.6 上的“错误配置选项:UseKeychain”

    我正在尝试在 Mac Mac OS Sierra 10 12 6 上设置 ssh 配置 以便将 ssh 密钥的密码存储在钥匙串中 以前我可以这样做 ssh add K ssh id rsa 但最近这个方法似乎不再起作用了 下列的本文 htt
  • USB 传输数据

    我正在尝试通过 USB 发送和接收数据 我的设备 Acer Iconia A500 拥有连接到设备所需的一切 一切都很好并且工作正常 但是当我尝试发送和接收数据时 它的行为并不像预期的 这是我的代码 for this is the main
  • git pull --rebase 上游 master 污染了我的 Github PR diff

    在按照维护人员的要求更改 PR 之前 我会执行以下操作git pull rebase upstream master将我的提交置于代码库中其他新提交之上 然而 这似乎污染我的 PR 的差异 https github com pandas d
  • preg_match() 和用户名

    function isUserID username if preg match a z d 2 20 i username return true else return false 简单的 我有这个 你能解释一下它检查什么吗 我知道它会
  • 无法使用不安全的私钥通过 ssh 连接到 vagrant VM(vagrant 1.7.2)

    我有一个包含 3 个虚拟机的集群 这是 Vagrant 文件 mode ruby vi set ft ruby hosts host0 gt 192 168 33 10 host1 gt 192 168 33 11 host2 gt 192
  • 如何在 C 中创建 3 位变量作为数据类型? [复制]

    这个问题在这里已经有答案了 I can typedef char to CHAR1这是8位 但是如何将 3 位变量设置为数据类型呢 您可能想要执行类似于以下操作的操作 struct unsigned int fieldof3bits 3 n
  • CSS Hack 针对 Firefox 3.5+?

    Firefox 3 5 现在支持 nth 伪类 我用它来定位 Safari 和 Chrome 的 css 现在 Firefox 也会读取这些内容 从而导致轻微的布局问题 有谁知道专门针对 FF 3 5 的方法吗 BODY nth of ty
  • 如何使用 WordPress 点击播放 GIF(如 9GaG.com)?

    我需要在wordpress网站中实现这个 gif播放器 因为gif页面高达6mb 所以性能真的很糟糕 我读过这个Onclick 使用 jQuery 播放 GIF 图像并从头开始 https stackoverflow com questio
  • 如何删除 angular-ui-router URL 中的“#”符号

    我正在使用 angular ui router 库 但我遇到了 URL 问题 我有以下代码 app js app config function stateProvider urlRouterProvider stateProvider s
  • 根据 getDay 和 getHours + getMinutes 显示 div

    我正在为一家广播电台建立一个网站 并想显示当前正在播出的主持人 我构建了一个网络应用程序 其中包含演示者的数据 姓名 照片 个人简介以及每个工作日的开始 结束时间 div div class slot div div div 12 00 1
  • 以编程方式检查 DataBound CheckListBox

    我有一个 DataBound CheckedListBox 我需要检查上面的一些项目 我尝试使用以下代码 if string IsNullOrEmpty search Languages string langs search Langua
  • 如何避免 Raphael js 路径重叠

    我有一个基于 Raphael js 的流程图 它是一样的http raphaeljs com graffle html http raphaeljs com graffle html 然而 由于我有许多矩形拉斐尔盒子 并且我的线条路径是直线
  • Android 应用程序上的阿拉伯日期

    我正在开发 Android 应用程序 以及在应用程序上添加阿拉伯语的下一个版本 但我有一个问题 这个问题是 Android操作系统转换日期动态地为阿拉伯格式 我在 URL 参数中使用它 服务器无法读取它 如何将任何阿拉伯日期转换为英文日期
  • 打开 .git/config :权限被拒绝

    C Users Inspiron git config文件夹为空且命令 git config user email email protected cdn cgi l email protection返回错误 打开 git config 权
  • Android RecyclerView 适配器在单元测试中给出 null

    我正在尝试使用 AndroidJunit4 测试 RecyclerView 这是我的测试代码 package com kaushik myredmart ui all includes RunWith AndroidJUnit4 class