Android 单元测试断言校验方法 org.junit.Assert

判断布尔值

assertTrue

assertFalse

判断对象非空

assertNull(object);

 案例:

PersistableBundle result = Util.getCarrierConfig(mockContext, subId);assertNull(result);

 

判断是否相等

assertEquals("mocked_string", result.toString());

package org.junit;public class Assert {/*** Asserts that two objects are equal. If they are not, an* {@link AssertionError} without a message is thrown. If* <code>expected</code> and <code>actual</code> are <code>null</code>,* they are considered equal.** @param expected expected value* @param actual the value to check against <code>expected</code>*/public static void assertEquals(Object expected, Object actual) {assertEquals(null, expected, actual);}
}

可以用于字符串比较,比如下面的查询条件拼接

//测试类
public class ApnSettingsTest {@Testpublic void testGetFillListQuery_initialWhereEmpty() {StringBuilder where = new StringBuilder();int subId = 1;// Mock the behavior of getSelection methodwhen(mFragment.getSelection("", subId)).thenReturn("mocked_selection");StringBuilder result = mFragment.getFillListQuery(where, subId);// Verify the `where` has the expected selectionassertEquals("mocked_selection", result.toString());// Check log output (Could use a library or a custom handler to intercept logs)// For simplicity, assuming log validation is part of the manual test validation process.}//TODO: 存在问题的测试接口,为什么其他两个getSelection可以返回预期??@Testpublic void testGetFillListQuery_initialWhereNonEmpty() {StringBuilder where = new StringBuilder("existing_condition");int subId = 1;// Mock the behavior of getSelection method,没生效?when(mFragment.getSelection("existing_condition", subId)).thenReturn(" AND mocked_selection");StringBuilder result = mFragment.getFillListQuery(where, subId);// Verify the `where` has the expected selection// 校验存在问题assertEquals("existing_condition AND mocked_selection", result.toString());// Check log output similarly as mentioned in previous test}@Testpublic void testGetFillListQuery_differentSubId() {StringBuilder where = new StringBuilder();int subId = 2;// Mock the behavior of getSelection methodwhen(mFragment.getSelection("", subId)).thenReturn("different_selection");StringBuilder result = mFragment.getFillListQuery(where, subId);// Verify the `where` has the expected selectionassertEquals("different_selection", result.toString());// Check log output similarly as mentioned in previous tests}}//被测试类
public class DemoSettings {@VisibleForTestingStringBuilder getFillListQuery(StringBuilder where, int subId) {String selection = "";selection = getSelection(selection, subId);where.append(selection);    // 拼接新增的过滤条件return where;}/*** Customize the cursor select conditions with subId releated to SIM.*/String getSelection(String selection, int subId) {boolean isCustomized = true;Context context = getContext();if (context != null) {isCustomized = SubscriptionManager.getResourcesForSubId(getContext().getApplicationContext(), subId).getBoolean(R.bool.config_is_customize_selection);}if(!isCustomized) return selection;// 注意有两个双引号的地方:第一个转义",第二个是字符串匹配// 拼接的查询语句等效于:// (type = 0 or sub_id = $传入值)// SQL 查询语句:// SELECT *// FROM your_table// WHERE status = 'active' AND (type = "0" OR sub_id = "subId");String where = "(" + " type =\"" + "0" + "\"";where += " or sub_id =\"" + subId + "\"";where += " or type =\"" + "2" + "\"" + ")";selection = selection + " and " + where;return where;}// Additional tests could be written to consider edge cases, such as:// - Negative subId values// - Null or invalid StringBuilder where value// In real scenario, depending on codebase other relevant checks can also be added.}

上述代码校验存在问题,报错如下:

Expected :existing_condition AND mocked_selection
Actual   :existing_condition and (type ="0" or sub_id ="1" or type ="2")
<Click to see difference>

org.junit.ComparisonFailure: expected:<existing_condition [AND mocked_selection]> but was:<existing_condition [and (type ="0" or sub_id ="1" or type ="2")]>

修改后pass的逻辑,因为内部会自己创建where值,不知道为什么不按 when.thenReturn 的返回 mocked_selection

    @Testpublic void testGetFillListQuery_initialWhereNonEmpty() {StringBuilder where = new StringBuilder("existing_condition");int subId = 1;// Mock the behavior of getSelection methodwhen(mFragment.getSelection("", subId)).thenReturn(" and (type ='0' or sub_id ='1' or type ='2')");StringBuilder result = mFragment.getFillListQuery(where, subId);// Verify the `where` has the expected selectionassertEquals("existing_condition and (type ='0' or sub_id ='1' or type  ='2')",result.toString());}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/63067.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Django异步视图adrf解决办法

提问 在Django编写异步视图的时候会出现 AssertionError: Expected a Response, HttpResponse or HttpStreamingResponse to be returned from the view 或者 TypeError: sync_to_async can only be applied to sync functions. 诸如此类的错误的时候一般发生在异步视图中…

Python语法之正则表达式详解以及re模块中的常用函数

正则表达式详解及re模块中的常用函数 概念、作用和步骤 概念&#xff1a; 本身也是一个字符串&#xff0c;其中的字符具有特殊含义&#xff0c;将来我们可以根据这个字符串【正则表达式】去处理其他的字符串&#xff0c;比如可以对其他字符串进行匹配&#xff0c;切分&#xf…

算法训练营day28(回溯算法04:复原IP地址,子集,子集2)

28 第七章 回溯算法 ● 93.复原IP地址 ● 78.子集 ● 90.子集II 详细布置 93.复原IP地址 本期本来是很有难度的&#xff0c;不过 大家做完 分割回文串 之后&#xff0c;本题就容易很多了 题目链接/文章讲解&#xff1a;https://programmercarl.com/0093.%E5%A4%8D%E5%8E%9F…

【机器学习02--模型评估】

机器学习 --- 模型评估 你需要得到更好的模型&#xff0c;怎么判断模型更好呢&#xff1f;你需要先得到训练集和测试集&#xff0c;怎么划分它们呢&#xff1f;训练完模型之后&#xff0c;在验证集上测试的时候&#xff0c;用什么指标衡量好坏呢&#xff1f;云里雾里&#xff0…

NPM镜像详解

NPM镜像详解 什么是NPM镜像 NPM镜像&#xff08;NPM Mirror&#xff09;是一个完整的NPM包的副本服务器。由于npm的官方registry服务器部署在国外&#xff0c;国内访问可能会比较慢&#xff0c;因此使用镜像可以加快包的下载速度。 常用的NPM镜像源 npm官方镜像 https://reg…

Linux 查看系统资源常用命令

目录 Linux 查看系统资源常用命令 一、top 二、htop 三、vmstat 四、iostat 五、mpstat 六、free 七、sar 八、ps 九、pstree 十、lsof 十一、uptime 十二、dmesg 十三、dmidecode 十四、lsblk 十五、blkid 十六、fdisk -l 十七、parted -l 十八、df -h 十…

【计算机网络】实验4:生成树协议STP的功能以及虚拟局域网VLAN

实验 4&#xff1a;生成树协议STP的功能以及虚拟局域网VLAN 一、 实验目的 加深对生成树协议STP的功能的理解。 了解虚拟局域网VLAN。 二、 实验环境 • Cisco Packet Tracer 模拟器 三、 实验内容 1、验证交换机生成树协议STP的功能 (1) 第一步&#xff1a;构建网络拓…

Linux中的常用基本指令(下)

Linux常用基本指令 Linux中的基本指令12.head指令13.tail指令简单解释重定向与管道(重要) 14.date指令(时间相关的指令)15.cal指令(不重要)16.find指令(灰常重要&#xff09;17.grep指令(重要)18.which指令和alias指令19.zip/unzip指令&#xff1a;20.tar指令&#xff08;重要&…

服务器数据恢复—硬盘掉线导致热备盘同步失败的RAID5阵列数据恢复案例

服务器存储数据恢复环境&#xff1a; 华为S5300存储中有12块FC硬盘&#xff0c;其中11块硬盘作为数据盘组建了一组RAID5阵列&#xff0c;剩下的1块硬盘作为热备盘使用。基于RAID的LUN分配给linux操作系统使用&#xff0c;存放的数据主要是Oracle数据库。 服务器存储故障&#…

Vue+vite 组件开发的环境准备

一.nodejs安装 进入Node.js 官网&#xff08;Node.js — Run JavaScript Everywhere&#xff09;&#xff0c;点击下载。 双击打开&#xff0c;进行安装 双击打开后&#xff0c;点击 next&#xff08;下一步&#xff09;,后面也是一直点击 next 无其他设置&#xff0c;直到 …

20241129解决在Ubuntu20.04下编译中科创达的CM6125的Android10出现找不到库文件libtinfo.so.5的问题

20241129解决在Ubuntu20.04下编译中科创达的CM6125的Android10出现找不到库文件libtinfo.so.5的问题 2024/11/29 20:41 缘起&#xff1a;中科创达的高通CM6125开发板的Android10的编译环境需要。 [ 11% 15993/135734] target Java source list: vr [ 11% 15994/135734] target …

react 路由鉴权

权限路由一般两种 1中接口中返回菜单 2 接口中返回权限&#xff0c;前端做匹配 一般都是那种结合&#xff0c;react中没有vue那种钩子函数如何做&#xff1f; 在项目中写一个高阶函数&#xff0c;在高阶函数中判断权限、是否登录等操作app.tsx或者man.tsx中使用 《AuthRouter》…

Monkey操作及问题场景具体样例日志分析

目录 1. 应用崩溃 (App Crashes) 2. ANR (Application Not Responding) 3. 内存问题 (Memory Issues) 4、生成及查看日志&#xff1a; 1. 应用崩溃 (App Crashes) Monkey操作导致的场景&#xff1a; 你可以使用Monkey发送大量的点击事件、滑动操作或按键操作来模拟极端的…

k8s集群中金丝雀发布 + 声明式资源管理yaml

一、K8S常见的发布方式 旨在降低发布风险并提高发布速度 1、蓝绿发布 两套环境&#xff08;设备&#xff09;交替升级&#xff0c;旧版本保留一定时间便于回滚 优点&#xff1a;对用户无感&#xff0c;是最安全的发布方式&#xff0c;业务稳定 缺点&#xff1a;需要两套系统&…

openssl的运用

一、概述 Opssl是一个用于TLS/SSL协议的工具包&#xff0c;也是一个通用密码库。 包含了国密sm2 sm3 sm4&#xff0c;包含了对称加密&#xff0c;非对称加密&#xff0c;单项散列&#xff0c;伪随机、签名&#xff0c;密码交换&#xff0c;证书等一些算法库。 为了深层次的学习…

基于SpringBoot+Vue的靓车汽车销售网站-无偿分享 (附源码+LW+调试)

目录 1. 项目技术 2. 功能菜单 3. 部分功能截图 4. 研究背景 5. 研究目的 6. 可行性分析 6.1 技术可行性 6.2 经济可行性 6.3 操作可行性 7. 系统设计 7.1 概述 7.2 系统流程和逻辑 7.3 系统结构 8. 数据库设计 8.1 数据库ER图 &#xff08;1&#xff09;材料分…

基于飞腾S2500处理器的全国产加固服务器

近日&#xff0c;西安康德航测电子科技有限公司凭借其深厚的行业底蕴和创新精神&#xff0c;正式推出了基于飞腾S2500处理器的全国产加固服务器。这一产品的问世&#xff0c;不仅标志着我国在信息技术领域的自立自强迈出了坚实的一步&#xff0c;更以其卓越的性能、坚固的设计和…

Ubuntu源码安装gitlab13.7集群多前端《二》

Ubuntu源码安装gitlab13.7《一》 gitaly需要调整的服务 redis socket->ipbind ....* # 0.0.0.0pg vim /etc/postgresql/14/main/pg_hba.confhost all all ..../32 md5gitaly vim /home/git/gitaly/config.tomlbin_dir "/home/gi…

OpenStack-Glance组件

Glance Glance使用磁盘格式和容器格式基础配置镜像转换 Glance 是 OpenStack 的镜像服务&#xff0c;负责存储、发现和管理虚拟机镜像。它允许用户创建和共享镜像&#xff0c;用于启动虚拟机实例。 Glance 的主要功能 &#xff08;1&#xff09;虚拟机镜像的管理 支持镜像的上…

C++编程:模拟实现CyberRT的DataVisitor和DataDispatcher

文章目录 0. 引言1. 设计概要1.1 主要组件1.2 类关系图1.3 工作流程 2. 代码实现2.1. 定义数据结构2.2. 实现 DataVisitor2.3. 实现 DataDispatcher2.4. 实现 Receiver2.5. 实现具体的 DataVisitor2.6. 示例主程序2.7. 编译和运行 0. 引言 使用 C 实现一个类似CyberRT 架构的 …