当前位置: 首页 > news >正文

kotlin + LiveData 测试

viewModel测试:https://developer.android.com/codelabs/basic-android-kotlin-compose-test-viewmodel#3

  androidTestImplementation "org.jetbrains.kotlin:kotlin-test:1.9.0"androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3'
import androidx.lifecycle.MutableLiveData
import androidx.test.espresso.action.ViewActions.*
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith@RunWith(AndroidJUnit4::class)
@LargeTest
class  LiveDataTest{@Testfun testLiveDataValueChange() = runTest {val liveData = MutableLiveData("1");var observedValue: String? = nulllaunch(Main){liveData.observeForever { value ->observedValue  = value}}// 在协程中进行异步操作val job  =   launch(Dispatchers.Default) {delay(1000)// 更改 LiveData 的值liveData.postValue("2")}job.join()assertEquals(observedValue, "2")assertEquals(liveData.value, "2")}}
http://www.lryc.cn/news/124453.html

相关文章:

  • 【dnf5文档】新一代RedHat自动化包管理器
  • 数据可视化工具的三大类报表制作流程分享
  • lua使用心得
  • Docker升级后,出现Error response from daemon: Unknown runtime specified docker-runc
  • [Poetize6] IncDec Sequence
  • 通过Microsoft Loopback Adapter实现虚拟机和物理机的通信
  • 算法leetcode|70. 爬楼梯(rust重拳出击)
  • 基于epoll的TCP服务器端(C++)
  • 实时安全分析监控加强网络安全
  • 基于ipad协议的gewe框架进行微信群组管理(二)
  • 大数据-玩转数据-Flink网页埋点PV统计
  • 什么是伪类选择器?
  • PLY模型格式详解【3D】
  • Java的反射机制、Lambda表达式和枚举
  • 数据结构:堆的实现
  • zabbix-6.4 监控 MySQL
  • 深入探索:解读创意的力量——idea的下载、初步使用
  • Redis详解
  • 【Linux】高级IO
  • 动态HTTP代理与竞争情报收集的关联
  • kafka基本概念及操作
  • 分享个试卷去笔迹什么软件,几个步骤轻松擦除
  • ClickHouse(十八):Clickhouse Integration系列表引擎
  • 日常BUG——代码提交到了本地但是没有push,删除了本地分支如何恢复
  • Markdown语法
  • vue3表格,编辑案例
  • SQL Server Reporting Services 报错:报表服务器无法访问服务帐户的私钥
  • QT报表Limereport v1.5.35编译及使用
  • 互联网发展历程:从中继器口不够到集线器的引入
  • vue+flask基于知识图谱的抑郁症问答系统