当前位置: 首页 > news >正文

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); 案例&#xff1a; PersistableBundle result Util.getCarrierConfig(mockContext, subId);assertNull(result); 判断是否相等 assertEquals("mocked_string", result.toString()); package or…...

亚马逊云(AWS)使用root用户登录

最近在AWS新开了服务器&#xff08;EC2&#xff09;&#xff0c;用于学习&#xff0c;遇到一个问题就是默认是用ec2-user用户登录&#xff0c;也需要密钥对。 既然是学习用的服务器&#xff0c;还是想直接用root登录&#xff0c;下面开始修改&#xff1a; 操作系统是&#xff1…...

用点云信息来进行监督目标检测

&#x1f351;个人主页&#xff1a;Jupiter. &#x1f680; 所属专栏&#xff1a;传知代码 欢迎大家点赞收藏评论&#x1f60a; 目录 概述问题分析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 响应状态码&#xff1a;管理和自定义 HTTP Status Code 本文介绍了如何在 FastAPI 中声明、使用和修改 HTTP 状态码&#xff0c;涵盖了常见的 HTTP 状态码分类&#xff0c;如信息响应&#xff08;1xx&#xff09;、成功状态&#xff08;2xx&#xff09;、客户端错误&a…...

【人工智能数学基础篇】线性代数基础学习:深入解读矩阵及其运算

矩阵及其运算&#xff1a;人工智能入门数学基础的深入解读 引言 线性代数是人工智能&#xff08;AI&#xff09;和机器学习的数学基础&#xff0c;而矩阵作为其核心概念之一&#xff0c;承担着数据表示、变换和运算的重任。矩阵不仅在数据科学中广泛应用&#xff0c;更是神经网…...

RNACOS:用Rust实现的Nacos服务

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

JAVA |日常开发中JSTL标签库详解

JAVA &#xff5c;日常开发中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的情况下&#xff0c;尝试消费一个HTTPS URL时会遇到以下测试失败&#xff1a; Test void whenHttpsUrlIsConsumed…...

Lighthouse(灯塔)—— Chrome 浏览器性能测试工具

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

扫二维码进小程序的指定页面

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

如何用IntelliJ IDEA开发Android Studio用自定义Gradle插件

博主所用软件版本为&#xff1a; IntelliJ IDEA 2024.1.4 (Community Edition) Android Studio Ladybug Feature Drop | 2024.2.2 Beta 1 1、制作gradle插件&#xff08;IntelliJ IDEA 2024.1.4&#xff09; 新建groovy工程&#xff0c;File–>New–>Project… 右键点…...

YOLOv8实战道路裂缝缺陷识别

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

RPC一分钟

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

Elasticsearch ILM 故障排除:常见问题及修复

作者&#xff1a;来自 Elastic Stef Nestor 大家好&#xff01;我们的 Elasticsearch 团队正在不断改进我们的索引生命周期管理 (index Lifecycle Management - ILM) 功能。当我第一次加入 Elastic Support 时&#xff0c;我通过我们的使用 ILM 实现自动滚动教程快速上手。在帮…...

Unity 设计模式-策略模式(Strategy Pattern)详解

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

【Maven系列】深入解析 Maven 常用命令

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

微信小程序之简单的数据中心管理平台(1)

微信小程序之简单的数据中心管理平台&#xff08;1&#xff09; 引言 随着微信小程序的广泛应用&#xff0c;越来越多的企业开始探索如何利用这一技术开发高效、便捷的管理平台。数据中心管理作为信息化建设的重要组成部分&#xff0c;需要一个灵活、可扩展的界面来实现资源的…...

sqlmap --os-shell的原理(MySQL,MSSQL,PostgreSQL,Oracle,SQLite)

1. MySQL 条件 数据库用户需要具备高权限&#xff08;如 FILE 权限&#xff09;。数据库服务运行用户需要对目标目录有写权限。Web 服务器有可写目录&#xff0c;且支持执行上传的脚本&#xff08;如 PHP、JSP 等&#xff09;。 原理 利用 MySQL 的 SELECT ... INTO OUTFIL…...

2024年认证杯SPSSPRO杯数学建模C题(第一阶段)云中的海盐解题全过程文档及程序

2024年认证杯SPSSPRO杯数学建模 C题 云中的海盐 原题再现&#xff1a; 巴黎气候协定提出的目标是&#xff1a;在2100年前&#xff0c;把全球平均气温相对于工业革命以前的气温升幅控制在不超过2摄氏度的水平&#xff0c;并为1.5摄氏度而努力。但事实上&#xff0c;许多之前的…...

IDEA运行Tomcat出现乱码问题解决汇总

最近正值期末周&#xff0c;有很多同学在写期末Java web作业时&#xff0c;运行tomcat出现乱码问题&#xff0c;经过多次解决与研究&#xff0c;我做了如下整理&#xff1a; 原因&#xff1a; IDEA本身编码与tomcat的编码与Windows编码不同导致&#xff0c;Windows 系统控制台…...

在Ubuntu中设置开机自动运行(sudo)指令的指南

在Ubuntu系统中&#xff0c;有时需要在系统启动时自动执行某些命令&#xff0c;特别是需要 sudo权限的指令。为了实现这一功能&#xff0c;可以使用多种方法&#xff0c;包括编写Systemd服务、配置 rc.local文件或使用 cron任务计划。本文将详细介绍这些方法&#xff0c;并提供…...

【android bluetooth 框架分析 04】【bt-framework 层详解 1】【BluetoothProperties介绍】

1. BluetoothProperties介绍 libsysprop/srcs/android/sysprop/BluetoothProperties.sysprop BluetoothProperties.sysprop 是 Android AOSP 中的一种 系统属性定义文件&#xff08;System Property Definition File&#xff09;&#xff0c;用于声明和管理 Bluetooth 模块相…...

C# 类和继承(抽象类)

抽象类 抽象类是指设计为被继承的类。抽象类只能被用作其他类的基类。 不能创建抽象类的实例。抽象类使用abstract修饰符声明。 抽象类可以包含抽象成员或普通的非抽象成员。抽象类的成员可以是抽象成员和普通带 实现的成员的任意组合。抽象类自己可以派生自另一个抽象类。例…...

多种风格导航菜单 HTML 实现(附源码)

下面我将为您展示 6 种不同风格的导航菜单实现&#xff0c;每种都包含完整 HTML、CSS 和 JavaScript 代码。 1. 简约水平导航栏 <!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><meta name"viewport&qu…...

DeepSeek 技术赋能无人农场协同作业:用 AI 重构农田管理 “神经网”

目录 一、引言二、DeepSeek 技术大揭秘2.1 核心架构解析2.2 关键技术剖析 三、智能农业无人农场协同作业现状3.1 发展现状概述3.2 协同作业模式介绍 四、DeepSeek 的 “农场奇妙游”4.1 数据处理与分析4.2 作物生长监测与预测4.3 病虫害防治4.4 农机协同作业调度 五、实际案例大…...

redis和redission的区别

Redis 和 Redisson 是两个密切相关但又本质不同的技术&#xff0c;它们扮演着完全不同的角色&#xff1a; Redis: 内存数据库/数据结构存储 本质&#xff1a; 它是一个开源的、高性能的、基于内存的 键值存储数据库。它也可以将数据持久化到磁盘。 核心功能&#xff1a; 提供丰…...

Linux 下 DMA 内存映射浅析

序 系统 I/O 设备驱动程序通常调用其特定子系统的接口为 DMA 分配内存&#xff0c;但最终会调到 DMA 子系统的dma_alloc_coherent()/dma_alloc_attrs() 等接口。 关于 dma_alloc_coherent 接口详细的代码讲解、调用流程&#xff0c;可以参考这篇文章&#xff0c;我觉得写的非常…...

aardio 自动识别验证码输入

技术尝试 上周在发学习日志时有网友提议“在网页上识别验证码”&#xff0c;于是尝试整合图像识别与网页自动化技术&#xff0c;完成了这套模拟登录流程。核心思路是&#xff1a;截图验证码→OCR识别→自动填充表单→提交并验证结果。 代码在这里 import soImage; import we…...

JavaScript 标签加载

目录 JavaScript 标签加载script 标签的 async 和 defer 属性&#xff0c;分别代表什么&#xff0c;有什么区别1. 普通 script 标签2. async 属性3. defer 属性4. type"module"5. 各种加载方式的对比6. 使用建议 JavaScript 标签加载 script 标签的 async 和 defer …...