测试 - 单元测试(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"
}
相关推荐
阿巴斯甜12 小时前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker12 小时前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq952713 小时前
Andorid Google 登录接入文档
android
黄林晴15 小时前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab1 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读
城东米粉儿1 天前
Android MediaPlayer 笔记
android
金銀銅鐵1 天前
浅解 JUnit 4 第十一篇:@Before 注解和 @After 注解如何发挥作用?
junit·单元测试
Jony_1 天前
Android 启动优化方案
android
阿巴斯甜1 天前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android
张小潇1 天前
AOSP15 Input专题InputReader源码分析
android