Skip to content

Commit

Permalink
Remove unused method and refactor Board Api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Lien committed Feb 1, 2021
1 parent 15a0094 commit 74cfb86
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 113 deletions.
14 changes: 14 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ android {
buildFeatures {
viewBinding true
}

sourceSets {
String sharedTestDir = 'src/sharedTest/java'
test {
java.srcDir sharedTestDir
}
androidTest {
java.srcDir sharedTestDir
}
}
}

def domain() {
Expand Down Expand Up @@ -129,11 +139,15 @@ dependencies {
testImplementation deps.google.truth
testImplementation deps.junit
testImplementation deps.mockK.core
testImplementation deps.androidX.arch
testImplementation deps.kotlin.coroutines.test

androidTestImplementation deps.androidX.test.ext.junit
androidTestImplementation deps.androidX.test.ext.espresso
androidTestImplementation deps.google.truth
androidTestImplementation deps.mockK.android
androidTestImplementation deps.androidX.arch
androidTestImplementation deps.kotlin.coroutines.test

debugImplementation deps.square.leakcanary

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/tw/y_studio/ptt/di/DataSourceModules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import tw.y_studio.ptt.source.remote.search.ISearchBoardRemoteDataSource
import tw.y_studio.ptt.source.remote.search.SearchBoardRemoteDataSourceImpl

val dataSourceModules = module {
factory<IPopularRemoteDataSource> { PopularRemoteDataSourceImpl(get(), get()) }
factory<IPopularRemoteDataSource> { PopularRemoteDataSourceImpl(get()) }
factory<ISearchBoardRemoteDataSource> { SearchBoardRemoteDataSourceImpl(get()) }
factory<IPostRemoteDataSource> { PostRemoteDataSourceImpl(get()) }
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package tw.y_studio.ptt.source.remote.board

import tw.y_studio.ptt.api.model.hot_board.HotBoard
import tw.y_studio.ptt.api.model.hot_board.HotBoardTemp

interface IPopularRemoteDataSource {

fun getPopularBoardData(page: Int, count: Int): MutableList<HotBoardTemp>

suspend fun getPopularBoards(): HotBoard

fun disposeAll()
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
package tw.y_studio.ptt.source.remote.board

import tw.y_studio.ptt.api.PopularBoardListAPI
import tw.y_studio.ptt.api.board.BoardApiService
import tw.y_studio.ptt.api.model.hot_board.HotBoard
import tw.y_studio.ptt.api.model.hot_board.HotBoardTemp

class PopularRemoteDataSourceImpl(
private val boardApiService: BoardApiService,
private val popularBoardListAPI: PopularBoardListAPI
private val boardApiService: BoardApiService
) : IPopularRemoteDataSource {

@Throws(Exception::class)
override fun getPopularBoardData(page: Int, count: Int): MutableList<HotBoardTemp> {
return popularBoardListAPI.refresh(page, count)
}

override suspend fun getPopularBoards(): HotBoard {
return boardApiService.getPopularBoard()
}

override fun disposeAll() {
popularBoardListAPI.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,5 @@ class HotBoardsViewModel(

override fun onCleared() {
super.onCleared()
popularRemoteDataSource.disposeAll()
}
}
29 changes: 29 additions & 0 deletions app/src/sharedTest/java/tw/y_studio/ptt/MainCoroutineRule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tw.y_studio.ptt

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestWatcher
import org.junit.runner.Description
import kotlin.coroutines.ContinuationInterceptor

/**
* Created by Michael.Lien
* on 2021/2/1
*/
@ExperimentalCoroutinesApi
class MainCoroutineRule : TestWatcher(), TestCoroutineScope by TestCoroutineScope() {

override fun starting(description: Description?) {
super.starting(description)
Dispatchers.setMain(this.coroutineContext[ContinuationInterceptor] as CoroutineDispatcher)
}

override fun finished(description: Description?) {
super.finished(description)
Dispatchers.resetMain()
}
}
27 changes: 27 additions & 0 deletions app/src/sharedTest/java/tw/y_studio/ptt/TestJsonFileUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tw.y_studio.ptt

import java.io.BufferedReader
import java.io.InputStreamReader

/**
* Created by Michael.Lien
* on 2021/2/1
*/
object TestJsonFileUtils {
fun loadJsonFile(fileName: String): String {
val classloader = javaClass.classLoader
val inputStream = classloader.getResourceAsStream(fileName)
val builder = StringBuilder()
val buffer = CharArray(1024)
val reader = BufferedReader(InputStreamReader(inputStream))
var n: Int
while (true) {
n = reader.read(buffer)
if (n < 0) break
builder.append(buffer, 0, n)
}
inputStream.close()

return builder.toString()
}
}
Original file line number Diff line number Diff line change
@@ -1,63 +1,66 @@
package tw.y_studio.ptt.source.remote.board

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.google.common.truth.Truth
import com.google.gson.Gson
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.coEvery
import io.mockk.impl.annotations.MockK
import org.json.JSONException
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import tw.y_studio.ptt.api.PopularBoardListAPI
import tw.y_studio.ptt.MainCoroutineRule
import tw.y_studio.ptt.TestJsonFileUtils
import tw.y_studio.ptt.api.board.BoardApiService
import tw.y_studio.ptt.api.model.hot_board.HotBoardTemp
import tw.y_studio.ptt.api.model.hot_board.HotBoard
import tw.y_studio.ptt.utils.fromJson

@ExperimentalCoroutinesApi
class PopularRemoteDataSourceTest {
private lateinit var popularRemoteDataSource: IPopularRemoteDataSource

@MockK
private lateinit var popularBoardListAPI: PopularBoardListAPI

@MockK
private lateinit var boardApi: BoardApiService

@get:Rule
var mainCoroutineRule = MainCoroutineRule()

@get:Rule
var instantExecutorRule = InstantTaskExecutorRule()

@Before
fun setUp() {
MockKAnnotations.init(this)
popularRemoteDataSource = PopularRemoteDataSourceImpl(boardApi, popularBoardListAPI)
popularRemoteDataSource = PopularRemoteDataSourceImpl(boardApi)
}

@Test
fun get_popular_board_data_then_return_data() {
fun get_popular_board_data_then_return_data() = runBlockingTest {
// GIVEN
val data = HotBoardTemp(1, "foo", "bar", 1, 1, "")
every { popularBoardListAPI.refresh(any(), any()) } returns mutableListOf(data)
val jsonFile = TestJsonFileUtils.loadJsonFile("api/board/popular_boards.json")
val hotBoard = Gson().fromJson<HotBoard>(jsonFile)

coEvery { boardApi.getPopularBoard() } returns hotBoard

// WHEN
val result = popularRemoteDataSource.getPopularBoardData(1, 10)
val result = popularRemoteDataSource.getPopularBoards()

// THEN
Truth.assertThat(result).apply {
isNotEmpty()
hasSize(1)
containsExactly(data)
isEqualTo(hotBoard)
}
}

private fun createSampleData() = mapOf("foo" to "bar")

@Test(expected = Exception::class)
fun get_popular_board_data_then_throw_exception() {
every { popularBoardListAPI.refresh(any(), any()) } throws Exception()

popularRemoteDataSource.getPopularBoardData(0, 0)
}

@Test(expected = JSONException::class)
fun get_popular_board_data_then_throw_json_exception() {
every { popularBoardListAPI.refresh(any(), any()) } throws JSONException("error!")
fun get_popular_board_data_then_throw_exception() = runBlockingTest {
coEvery { boardApi.getPopularBoard() } throws Exception()

popularRemoteDataSource.getPopularBoardData(0, 0)
popularRemoteDataSource.getPopularBoards()
}

@After
Expand Down
23 changes: 23 additions & 0 deletions app/src/test/resources/api/board/popular_boards.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"list": [
{
"bid": "string",
"brdname": "string",
"title": "string",
"flag": 0,
"type": "string",
"class": "string",
"nuser": 0,
"moderators": [
"string"
],
"reason": "string",
"read": true,
"total": 0,
"last_post_time": 0,
"stat_attr": 0,
"level_idx": "string"
}
],
"next_idx": "string"
}
Loading

0 comments on commit 74cfb86

Please sign in to comment.