当前位置: 首页 > 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;许多之前的…...

wordpress后台更新后 前端没变化的解决方法

使用siteground主机的wordpress网站&#xff0c;会出现更新了网站内容和修改了php模板文件、js文件、css文件、图片文件后&#xff0c;网站没有变化的情况。 不熟悉siteground主机的新手&#xff0c;遇到这个问题&#xff0c;就很抓狂&#xff0c;明明是哪都没操作错误&#x…...

SkyWalking 10.2.0 SWCK 配置过程

SkyWalking 10.2.0 & SWCK 配置过程 skywalking oap-server & ui 使用Docker安装在K8S集群以外&#xff0c;K8S集群中的微服务使用initContainer按命名空间将skywalking-java-agent注入到业务容器中。 SWCK有整套的解决方案&#xff0c;全安装在K8S群集中。 具体可参…...

RocketMQ延迟消息机制

两种延迟消息 RocketMQ中提供了两种延迟消息机制 指定固定的延迟级别 通过在Message中设定一个MessageDelayLevel参数&#xff0c;对应18个预设的延迟级别指定时间点的延迟级别 通过在Message中设定一个DeliverTimeMS指定一个Long类型表示的具体时间点。到了时间点后&#xf…...

【WiFi帧结构】

文章目录 帧结构MAC头部管理帧 帧结构 Wi-Fi的帧分为三部分组成&#xff1a;MAC头部frame bodyFCS&#xff0c;其中MAC是固定格式的&#xff0c;frame body是可变长度。 MAC头部有frame control&#xff0c;duration&#xff0c;address1&#xff0c;address2&#xff0c;addre…...

day52 ResNet18 CBAM

在深度学习的旅程中&#xff0c;我们不断探索如何提升模型的性能。今天&#xff0c;我将分享我在 ResNet18 模型中插入 CBAM&#xff08;Convolutional Block Attention Module&#xff09;模块&#xff0c;并采用分阶段微调策略的实践过程。通过这个过程&#xff0c;我不仅提升…...

Unsafe Fileupload篇补充-木马的详细教程与木马分享(中国蚁剑方式)

在之前的皮卡丘靶场第九期Unsafe Fileupload篇中我们学习了木马的原理并且学了一个简单的木马文件 本期内容是为了更好的为大家解释木马&#xff08;服务器方面的&#xff09;的原理&#xff0c;连接&#xff0c;以及各种木马及连接工具的分享 文件木马&#xff1a;https://w…...

Angular微前端架构:Module Federation + ngx-build-plus (Webpack)

以下是一个完整的 Angular 微前端示例&#xff0c;其中使用的是 Module Federation 和 npx-build-plus 实现了主应用&#xff08;Shell&#xff09;与子应用&#xff08;Remote&#xff09;的集成。 &#x1f6e0;️ 项目结构 angular-mf/ ├── shell-app/ # 主应用&…...

短视频矩阵系统文案创作功能开发实践,定制化开发

在短视频行业迅猛发展的当下&#xff0c;企业和个人创作者为了扩大影响力、提升传播效果&#xff0c;纷纷采用短视频矩阵运营策略&#xff0c;同时管理多个平台、多个账号的内容发布。然而&#xff0c;频繁的文案创作需求让运营者疲于应对&#xff0c;如何高效产出高质量文案成…...

Go语言多线程问题

打印零与奇偶数&#xff08;leetcode 1116&#xff09; 方法1&#xff1a;使用互斥锁和条件变量 package mainimport ("fmt""sync" )type ZeroEvenOdd struct {n intzeroMutex sync.MutexevenMutex sync.MutexoddMutex sync.Mutexcurrent int…...

从“安全密码”到测试体系:Gitee Test 赋能关键领域软件质量保障

关键领域软件测试的"安全密码"&#xff1a;Gitee Test如何破解行业痛点 在数字化浪潮席卷全球的今天&#xff0c;软件系统已成为国家关键领域的"神经中枢"。从国防军工到能源电力&#xff0c;从金融交易到交通管控&#xff0c;这些关乎国计民生的关键领域…...