测试 - 单元测试(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"
}
相关推荐
wxson728214 小时前
【Android视频监控系统技术总结】
android·音视频
hunterandroid15 小时前
[Android 从零到一] LiveData 粘性事件与单次消费:从重复触发到可靠事件总线
android
hunterandroid15 小时前
[Android 从零到一] Android 事件分发机制:从 ACTION_DOWN 到 View 事件消费链路
android
阿巴斯甜16 小时前
adb‑shell 全套命令实操总结
android
奔跑的架构师17 小时前
[A-49]ARMv9/v8-PSCI接口规范与工作流程简介
android·linux·arm开发·arm
AFinalStone17 小时前
Android 7系统国际化(七)Resources 与 AssetManager 源码剖析
android·国际化·local
Android-Flutter18 小时前
Android 内存泄漏详解
android·kotlin
catchadmin18 小时前
Fiber 与 PHP 8.6 Polling API 异步初探
android·开发语言·php
Android-Flutter18 小时前
android Handler , Looper , Message , MessageQueue 详解
android·kotlin