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());}
相关文章:

Android 单元测试断言校验方法 org.junit.Assert
判断布尔值 assertTrue assertFalse 判断对象非空 assertNull(object); 案例: PersistableBundle result Util.getCarrierConfig(mockContext, subId);assertNull(result); 判断是否相等 assertEquals("mocked_string", result.toString()); package or…...

亚马逊云(AWS)使用root用户登录
最近在AWS新开了服务器(EC2),用于学习,遇到一个问题就是默认是用ec2-user用户登录,也需要密钥对。 既然是学习用的服务器,还是想直接用root登录,下面开始修改: 操作系统是࿱…...

用点云信息来进行监督目标检测
🍑个人主页:Jupiter. 🚀 所属专栏:传知代码 欢迎大家点赞收藏评论😊 目录 概述问题分析Making Lift-splat work well is hard深度不准确深度过拟合不准确的BEV语义 模型总体框架显性深度监督 深度细化模块演示效果核心…...

Navicat连接服务器MySQL
Navicat连接服务器MySQL 1. Navicat连接服务器MySQL2. 如何查看MySQL用户名和密码3. 修改MySQL登录密码4. 安装MySQL(Centos7)遇到错误和问题 1. error 1045 (28000): access denied for user ‘root’‘localhost’ (using password:yes) 1. Navicat连接服务器MySQL 选择数据…...

FastAPI 响应状态码:管理和自定义 HTTP Status Code
FastAPI 响应状态码:管理和自定义 HTTP Status Code 本文介绍了如何在 FastAPI 中声明、使用和修改 HTTP 状态码,涵盖了常见的 HTTP 状态码分类,如信息响应(1xx)、成功状态(2xx)、客户端错误&a…...
【人工智能数学基础篇】线性代数基础学习:深入解读矩阵及其运算
矩阵及其运算:人工智能入门数学基础的深入解读 引言 线性代数是人工智能(AI)和机器学习的数学基础,而矩阵作为其核心概念之一,承担着数据表示、变换和运算的重任。矩阵不仅在数据科学中广泛应用,更是神经网…...

RNACOS:用Rust实现的Nacos服务
RNACOS是一个使用Rust语言开发的Nacos服务实现,它继承了Nacos的所有核心功能,并在此基础上进行了优化和改进。作为一个轻量级、快速、稳定且高性能的服务,RNACOS不仅包含了注册中心、配置中心和Web管理控制台的功能,还支持单机和集…...

JAVA |日常开发中JSTL标签库详解
JAVA |日常开发中JSTL标签库详解 前言一、JSTL 概述1.1 定义1.2 优势 二、JSTL 核心标签库2.1 导入 JSTL 库2.2 <c:out>标签 - 输出数据2.3 <c:if>标签 - 条件判断2.4 <c:choose>、<c:when>和<c:otherwise>标签 - 多条件选择 结束语优…...
Apache HttpClient 4和5访问没有有效证书的HTTPS
本文将展示如何配置Apache HttpClient 4和5以支持“接受所有”SSL。 目标很简单——访问没有有效证书的HTTPS URL。 SSLPeerUnverifiedException 在未配置SSL的情况下,尝试消费一个HTTPS URL时会遇到以下测试失败: Test void whenHttpsUrlIsConsumed…...

Lighthouse(灯塔)—— Chrome 浏览器性能测试工具
1.认识 Lighthouse Lighthouse 是 Google 开发的一款开源性能测试工具,用于分析网页或 Web 应用的性能、可访问性、最佳实践、安全性以及 SEO 等关键指标。开发人员可以通过 Lighthouse 快速了解网页的性能瓶颈,并基于优化建议进行改进。 核心功能&…...

扫二维码进小程序的指定页面
草料二维码解码器 微信开发者工具 获取二维码解码的参数->是否登陆->跳转 options.q onLoad: function (options) {// console.log("options",options.q)if (options && options.q) {// 解码二维码携带的链接信息let qrUrl decodeURIComponent(optio…...

如何用IntelliJ IDEA开发Android Studio用自定义Gradle插件
博主所用软件版本为: IntelliJ IDEA 2024.1.4 (Community Edition) Android Studio Ladybug Feature Drop | 2024.2.2 Beta 1 1、制作gradle插件(IntelliJ IDEA 2024.1.4) 新建groovy工程,File–>New–>Project… 右键点…...

YOLOv8实战道路裂缝缺陷识别
本文采用YOLOv8作为核心算法框架,结合PyQt5构建用户界面,使用Python3进行开发。YOLOv8以其高效的实时检测能力,在多个目标检测任务中展现出卓越性能。本研究针对道路裂缝数据集进行训练和优化,该数据集包含丰富的道路裂缝图像样本…...

RPC一分钟
概述 微服务治理:Springcloud、Dubbo服务通信:Grpc、Trift Dubbo 参考 Dubbo核心功能,主要提供了:远程方法调用、智能容错和负载均衡、提供服务自动注册、自动发现等高效服务治理功能。 Dubbo协议Dubbo支持dubbo、rmi、http、…...

Elasticsearch ILM 故障排除:常见问题及修复
作者:来自 Elastic Stef Nestor 大家好!我们的 Elasticsearch 团队正在不断改进我们的索引生命周期管理 (index Lifecycle Management - ILM) 功能。当我第一次加入 Elastic Support 时,我通过我们的使用 ILM 实现自动滚动教程快速上手。在帮…...

Unity 设计模式-策略模式(Strategy Pattern)详解
策略模式(Strategy Pattern)是一种行为型设计模式,定义了一系列算法,并将每种算法封装到独立的类中,使得它们可以互相替换。策略模式让算法可以在不影响客户端的情况下独立变化,客户端通过与这些策略对象进…...

【Maven系列】深入解析 Maven 常用命令
前言 在当今的软件开发过程中,项目管理是至关重要的一环。项目管理包括了项目构建、依赖管理以及发布部署等诸多方面。而在Java生态系统中,Maven已经成为了最受欢迎的项目管理工具之一。Maven 是一套用于构建、依赖管理和项目管理的工具,主要…...

微信小程序之简单的数据中心管理平台(1)
微信小程序之简单的数据中心管理平台(1) 引言 随着微信小程序的广泛应用,越来越多的企业开始探索如何利用这一技术开发高效、便捷的管理平台。数据中心管理作为信息化建设的重要组成部分,需要一个灵活、可扩展的界面来实现资源的…...
sqlmap --os-shell的原理(MySQL,MSSQL,PostgreSQL,Oracle,SQLite)
1. MySQL 条件 数据库用户需要具备高权限(如 FILE 权限)。数据库服务运行用户需要对目标目录有写权限。Web 服务器有可写目录,且支持执行上传的脚本(如 PHP、JSP 等)。 原理 利用 MySQL 的 SELECT ... INTO OUTFIL…...

2024年认证杯SPSSPRO杯数学建模C题(第一阶段)云中的海盐解题全过程文档及程序
2024年认证杯SPSSPRO杯数学建模 C题 云中的海盐 原题再现: 巴黎气候协定提出的目标是:在2100年前,把全球平均气温相对于工业革命以前的气温升幅控制在不超过2摄氏度的水平,并为1.5摄氏度而努力。但事实上,许多之前的…...
前端倒计时误差!
提示:记录工作中遇到的需求及解决办法 文章目录 前言一、误差从何而来?二、五大解决方案1. 动态校准法(基础版)2. Web Worker 计时3. 服务器时间同步4. Performance API 高精度计时5. 页面可见性API优化三、生产环境最佳实践四、终极解决方案架构前言 前几天听说公司某个项…...
测试markdown--肇兴
day1: 1、去程:7:04 --11:32高铁 高铁右转上售票大厅2楼,穿过候车厅下一楼,上大巴车 ¥10/人 **2、到达:**12点多到达寨子,买门票,美团/抖音:¥78人 3、中饭&a…...
Qwen3-Embedding-0.6B深度解析:多语言语义检索的轻量级利器
第一章 引言:语义表示的新时代挑战与Qwen3的破局之路 1.1 文本嵌入的核心价值与技术演进 在人工智能领域,文本嵌入技术如同连接自然语言与机器理解的“神经突触”——它将人类语言转化为计算机可计算的语义向量,支撑着搜索引擎、推荐系统、…...

2025 后端自学UNIAPP【项目实战:旅游项目】6、我的收藏页面
代码框架视图 1、先添加一个获取收藏景点的列表请求 【在文件my_api.js文件中添加】 // 引入公共的请求封装 import http from ./my_http.js// 登录接口(适配服务端返回 Token) export const login async (code, avatar) > {const res await http…...

Linux --进程控制
本文从以下五个方面来初步认识进程控制: 目录 进程创建 进程终止 进程等待 进程替换 模拟实现一个微型shell 进程创建 在Linux系统中我们可以在一个进程使用系统调用fork()来创建子进程,创建出来的进程就是子进程,原来的进程为父进程。…...
C++.OpenGL (20/64)混合(Blending)
混合(Blending) 透明效果核心原理 #mermaid-svg-SWG0UzVfJms7Sm3e {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-icon{fill:#552222;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-text{fill…...
解决:Android studio 编译后报错\app\src\main\cpp\CMakeLists.txt‘ to exist
现象: android studio报错: [CXX1409] D:\GitLab\xxxxx\app.cxx\Debug\3f3w4y1i\arm64-v8a\android_gradle_build.json : expected buildFiles file ‘D:\GitLab\xxxxx\app\src\main\cpp\CMakeLists.txt’ to exist 解决: 不要动CMakeLists.…...

spring Security对RBAC及其ABAC的支持使用
RBAC (基于角色的访问控制) RBAC (Role-Based Access Control) 是 Spring Security 中最常用的权限模型,它将权限分配给角色,再将角色分配给用户。 RBAC 核心实现 1. 数据库设计 users roles permissions ------- ------…...

WebRTC调研
WebRTC是什么,为什么,如何使用 WebRTC有什么优势 WebRTC Architecture Amazon KVS WebRTC 其它厂商WebRTC 海康门禁WebRTC 海康门禁其他界面整理 威视通WebRTC 局域网 Google浏览器 Microsoft Edge 公网 RTSP RTMP NVR ONVIF SIP SRT WebRTC协…...

HTTPS证书一年多少钱?
HTTPS证书作为保障网站数据传输安全的重要工具,成为众多网站运营者的必备选择。然而,面对市场上种类繁多的HTTPS证书,其一年费用究竟是多少,又受哪些因素影响呢? 首先,HTTPS证书通常在PinTrust这样的专业平…...