测试 - 单元测试(JUnit)

JUnit 官网

Mockito 官网

一、概念

1.1 注解

|------------------------|--------------------------------------|
| @Test | 标记一个函数为测试方法。 |
| @BeforeEach、@AfterEach | 在每个测试方法 前/后 执行,用于 准备/清理 运行环境。 |
| @BeforeAll、@AfterAll | 在所有测试 前/后 执行(必须是静态方法),用于 执行/释放 耗时操作。 |
| @Disabled | 禁用 / 忽略 / 不执行该方法。 |
| @DisplayName | 对 类/方法 取别名。 |

1.2 断言方法

|--------------------------------------------------------------------------------------------------------------------------------------------------------|------|
| assertEquals(expected, actual) 值 assertSame(expected, actual) 对象 assertNotSame(unexpected, actual) 对象 assertArrayEquals(expectedArray, actualArray) 数组 | 相等 |
| assertTrue(condition) assertFalse(condition) | 布尔 |
| assertNull(object) assertNotNull(object) | null |

二、使用

2.1 添加依赖

Mockito-Kotlin 最新版本

Kotlin 复制代码
dependencies {
  // Required -- JUnit 4 framework
  testImplementation "junit:junit:$jUnitVersion"
  // Optional -- Robolectric environment
  testImplementation "androidx.test:core:$androidXTestVersion"
  // Optional -- Mockito framework
  testImplementation "org.mockito:mockito-core:$mockitoVersion"
  // Optional -- mockito-kotlin
  testImplementation "org.mockito.kotlin:mockito-kotlin:$mockitoKotlinVersion"
  // Optional -- Mockk framework
  testImplementation "io.mockk:mockk:$mockkVersion"
}
相关推荐
CS_Zero1 小时前
安卓开发:Linux的android工具命令
android·linux·运维
fellow991 小时前
DSH Desktop - Android Edition
android·agent·deepseek·harness·dsh
Carson带你学Android1 小时前
Android Studio Rabbit 2 发布:除了Gemini,40+ AI Agent 正式入驻!
android·ai编程
三少爷的鞋14 小时前
Java 线程自上而下,Kotlin 协程自下而上
android
好的佩奇15 小时前
Dart 之增强枚举
android·flutter·dart
梦影_16 小时前
MySQL进阶教程(五)—— SQL调优(下)
android·大数据·数据库·后端·sql·mysql·database
好的佩奇18 小时前
蓝牙之无线电与通信距离
android·flutter·蓝牙
vilya19 小时前
我把一套完整的 Python Agent 运行时塞进手机 APK:依赖闭包、Rust 轮子和三个真机坑
android
呆呆敲代码的小Y20 小时前
【实战精选面试题】C# 面试题详细解答,按难易程度排序
android·java·游戏·c#·游戏开发·unity面试题·c#面试题
CarIise1 天前
Spring 框架:多配置文件、开发方式与 IOC 注解
junit·maven·jar