Man*_*ath 5 android unit-testing android-looper rx-java2
趋势视图模型测试
@RunWith(JUnit4::class)
class TrendingViewModelTest {
private lateinit var trendingRepository: TrendingRepository
private lateinit var trendingViewModel: TrendingViewModel
@get:Rule
val schedulers = RxImmediateSchedulerRule()
@Before
fun setUp() {
trendingRepository = mock(TrendingRepository::class.java)
trendingViewModel = TrendingViewModel(trendingRepository)
}
@Test
fun testWithNetwork() {
trendingViewModel.isConnected = true
trendingViewModel.fetchTrendingRepos()
verify(trendingRepository, times(1)).getTrendingRepos()
}
//...
}
Run Code Online (Sandbox Code Playgroud)
趋势视图模型
fun fetchTrendingRepos() {
if (isConnected) {
loadingProgress.value = true
compositeDisposable.add(
trendingRepository.getTrendingRepos().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ response ->
run {
loadingProgress.value = false
},
{ error ->
loadingProgress.value = false
}
)
)
}
Run Code Online (Sandbox Code Playgroud)
RxImmediateSchedulerRule:
class RxImmediateSchedulerRule : TestRule {
override fun apply(base: Statement?, description: Description?): Statement {
return object : Statement() {
@Throws(Throwable::class)
override fun evaluate() {
RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
RxJavaPlugins.setComputationSchedulerHandler { Schedulers.trampoline() }
RxJavaPlugins.setNewThreadSchedulerHandler { Schedulers.trampoline() }
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
try {
base?.evaluate()
} finally {
RxJavaPlugins.reset()
RxAndroidPlugins.reset()
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
TrendingRepositoryImpl:
class TrendingRepositoryImpl @Inject constructor(
val apiService: GitHubApi,
val trendingDao: AppDao
) : TrendingRepository {
override fun getTrendingRepos(): Single<List<TrendingRepo>> {
return apiService.getTrendingGits()
}
}
Run Code Online (Sandbox Code Playgroud)
趋势库:
interface TrendingRepository {
fun getTrendingRepos(): Single<List<TrendingRepo>>
}
Run Code Online (Sandbox Code Playgroud)
在fetchTrendingRepos()启动 Rxjava 调用时,它还挂钩到可能是导致它的原因的“AndroidSchedulers.mainThread()”。
java.lang.RuntimeException:未模拟 android.os.Looper 中的方法 getMainLooper。在 android.os.Looper.getMainLooper(Looper.java) 在 androidx.arch.core.executor.DefaultTaskExecutor.isMainThread(DefaultTaskExecutor.java:77) 在 androidx.arch.core.executor.ArchTaskExecutor.isMainThread(ArchTaskExecutor.java:116) ) 在 androidx.lifecycle.LiveData.assertMainThread(LiveData.java:461) 在 androidx.lifecycle.LiveData.setValue(LiveData.java:304) 在 androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50) 在 com.manoj .trendgitz.mvvm.ui.TrendingViewModel.fetchTrendingRepos(TrendingViewModel.kt:32) at com.manoj.trendgitz.TrendingViewModelTest.testWithNetwork(TrendingViewModelTest.kt:52) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.ref .NativeMethodAccessorImpl。
Nat*_*yev 14
当您更新LiveData值时,您还应该添加@get:Rule var rule: TestRule = InstantTaskExecutorRule()。
不要忘记在build.gradle文件中添加以下内容:
dependencies {
// ...
testImplementation "androidx.arch.core:core-testing:2.1.0"
}
Run Code Online (Sandbox Code Playgroud)
此外,相应地更改您的测试代码以避免NullPointerException:
@Test
fun testWithNetwork() {
trendingViewModel.isConnected = true
Mockito.`when`(trendingRepository.fetchTrendingRepos()).thenReturn(Single.just(listOf<TrendingRepo>()))
trendingViewModel.fetchTrendingRepos()
verify(trendingRepository, times(1)).getTrendingRepos()
}
Run Code Online (Sandbox Code Playgroud)
Mockito.when()每次调用模拟方法时,您都可以执行不同的操作。如果您不使用它,NullPointerException根据您的测试功能,您可能会看到可能。
在应用程序的 build.gradle 中的 android {} 下添加以下行
testOptions {
// Used for Unit testing Android dependent elements in /test folder
unitTests.includeAndroidResources = true
unitTests.returnDefaultValues = true
}
Run Code Online (Sandbox Code Playgroud)
小智 8
首先testImplementation添加core-testing.
testImplementation 'androidx.arch.core:core-testing:2.1.0'
Run Code Online (Sandbox Code Playgroud)
然后在您的单元测试类中添加一个InstantTaskExecutorRuleat 变量rule。
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
class TrendingViewModelTest {
@get:Rule
val rule = InstantTaskExecutorRule()
}
Run Code Online (Sandbox Code Playgroud)