Skip to content

Commit

Permalink
test: 테스트 코드 작성을 위한 기본 세팅 (#255)
Browse files Browse the repository at this point in the history
* feat: CoroutinesTestExtension 구현

* feat: Livedata getOrAwaitValue 구현

* feat: InstantTaskExecutorExtension 구현

* feat: TestFixture 생성

* style: ktlint 적용
  • Loading branch information
chaehyuns authored Aug 8, 2024
1 parent 8a20ff3 commit e60360b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 16 deletions.
16 changes: 0 additions & 16 deletions android/app/src/test/java/com/zzang/chongdae/ExampleUnitTest.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.zzang.chongdae.util

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestDispatcher
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.jupiter.api.extension.AfterEachCallback
import org.junit.jupiter.api.extension.BeforeEachCallback
import org.junit.jupiter.api.extension.ExtensionContext

@ExperimentalCoroutinesApi
class CoroutinesTestExtension(
private val dispatcher: TestDispatcher = UnconfinedTestDispatcher(),
) : BeforeEachCallback, AfterEachCallback {
override fun beforeEach(context: ExtensionContext?) {
Dispatchers.setMain(dispatcher)
}

override fun afterEach(context: ExtensionContext?) {
Dispatchers.resetMain()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.zzang.chongdae.util

import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException

fun <T> LiveData<T>.getOrAwaitValue(
time: Long = 2,
timeUnit: TimeUnit = TimeUnit.SECONDS,
): T {
var data: T? = null
val latch = CountDownLatch(1)
val observer =
object : Observer<T> {
override fun onChanged(value: T) {
data = value
latch.countDown()
this@getOrAwaitValue.removeObserver(this)
}
}

this.observeForever(observer)

// Don't wait indefinitely if the LiveData is not set.
if (!latch.await(time, timeUnit)) {
throw TimeoutException("LiveData value was never set.")
}

@Suppress("UNCHECKED_CAST")
return data as T
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.zzang.chongdae.util

import androidx.arch.core.executor.ArchTaskExecutor
import androidx.arch.core.executor.TaskExecutor
import org.junit.jupiter.api.extension.AfterEachCallback
import org.junit.jupiter.api.extension.BeforeEachCallback
import org.junit.jupiter.api.extension.ExtensionContext

class InstantTaskExecutorExtension : BeforeEachCallback, AfterEachCallback {
override fun beforeEach(context: ExtensionContext?) {
ArchTaskExecutor.getInstance().setDelegate(
object : TaskExecutor() {
override fun executeOnDiskIO(runnable: Runnable) {
runnable.run()
}

override fun postToMainThread(runnable: Runnable) {
runnable.run()
}

override fun isMainThread(): Boolean {
return true
}
},
)
}

override fun afterEach(context: ExtensionContext?) {
ArchTaskExecutor.getInstance().setDelegate(null)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.zzang.chongdae.util

object TestFixture

0 comments on commit e60360b

Please sign in to comment.