当前位置: 首页 > 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());}

http://www.lryc.cn/news/498039.html

相关文章:

  • 亚马逊云(AWS)使用root用户登录
  • 用点云信息来进行监督目标检测
  • Navicat连接服务器MySQL
  • FastAPI 响应状态码:管理和自定义 HTTP Status Code
  • 【人工智能数学基础篇】线性代数基础学习:深入解读矩阵及其运算
  • RNACOS:用Rust实现的Nacos服务
  • JAVA |日常开发中JSTL标签库详解
  • Apache HttpClient 4和5访问没有有效证书的HTTPS
  • Lighthouse(灯塔)—— Chrome 浏览器性能测试工具
  • 扫二维码进小程序的指定页面
  • 如何用IntelliJ IDEA开发Android Studio用自定义Gradle插件
  • YOLOv8实战道路裂缝缺陷识别
  • RPC一分钟
  • Elasticsearch ILM 故障排除:常见问题及修复
  • Unity 设计模式-策略模式(Strategy Pattern)详解
  • 【Maven系列】深入解析 Maven 常用命令
  • 微信小程序之简单的数据中心管理平台(1)
  • sqlmap --os-shell的原理(MySQL,MSSQL,PostgreSQL,Oracle,SQLite)
  • 2024年认证杯SPSSPRO杯数学建模C题(第一阶段)云中的海盐解题全过程文档及程序
  • 三维扫描检测在汽车制造中的应用
  • 【NoSQL数据库】Hbase基本操作——数据库表的增删改查
  • 【C++】格式化输出详解:掌握 cout 的进阶用法
  • 设计模式学习思路二
  • 什么是等级保护
  • k8s api对象,CRD
  • 【C++指南】C++内存管理 深度解析
  • C++小碗菜之二:软件单元测试
  • PyCharm+Selenium+Pytest配置小记
  • 摩尔线程 国产显卡 MUSA 并行编程 学习笔记-2024/12/04
  • 【FAQ】HarmonyOS SDK 闭源开放能力 —Remote Communication Kit