-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PracticeA3-통합 테스트 맛보기 (Test Double, Hilt)
- Loading branch information
Showing
7 changed files
with
162 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
app/src/androidTest/java/com/qualitybitz/booksearchapp/HiltTestRunner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.qualitybitz.booksearchapp | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import androidx.test.runner.AndroidJUnitRunner | ||
import dagger.hilt.android.testing.HiltTestApplication | ||
|
||
class HiltTestRunner : AndroidJUnitRunner() { | ||
|
||
override fun newApplication( | ||
cl: ClassLoader?, | ||
className: String?, | ||
context: Context? | ||
): Application { | ||
return super.newApplication(cl, HiltTestApplication::class.java.name, context) | ||
} | ||
} |
29 changes: 19 additions & 10 deletions
29
app/src/androidTest/java/com/qualitybitz/booksearchapp/data/db/BookSearchDaoTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
app/src/androidTest/java/com/qualitybitz/booksearchapp/di/TestAppModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.qualitybitz.booksearchapp.di | ||
|
||
import android.content.Context | ||
import androidx.room.Room | ||
import com.qualitybitz.booksearchapp.data.db.BookSearchDatabase | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Named | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object TestAppModule { | ||
|
||
@Provides | ||
@Named("test_db") | ||
fun provideInMemoryDb(@ApplicationContext context: Context): BookSearchDatabase = | ||
Room.inMemoryDatabaseBuilder(context, BookSearchDatabase::class.java) | ||
.allowMainThreadQueries() | ||
.build() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
app/src/test/java/com/qualitybitz/booksearchapp/data/repository/FakeBookSearchRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.qualitybitz.booksearchapp.data.repository | ||
|
||
import androidx.paging.PagingData | ||
import com.qualitybitz.booksearchapp.data.model.Book | ||
import com.qualitybitz.booksearchapp.data.model.SearchResponse | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flowOf | ||
import retrofit2.Response | ||
|
||
class FakeBookSearchRepository : BookSearchRepository { | ||
|
||
private val bookItems = mutableListOf<Book>() | ||
|
||
override suspend fun searchBooks( | ||
query: String, | ||
sort: String, | ||
page: Int, | ||
size: Int | ||
): Response<SearchResponse> { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun insertBooks(book: Book) { | ||
bookItems.add(book) | ||
} | ||
|
||
override suspend fun deleteBooks(book: Book) { | ||
bookItems.remove(book) | ||
} | ||
|
||
override fun getFavoriteBooks(): Flow<List<Book>> { | ||
return flowOf(bookItems) | ||
} | ||
|
||
override suspend fun saveSortMode(mode: String) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun getSortMode(): Flow<String> { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun saveCacheDeleteMode(mode: Boolean) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun getCacheDeleteMode(): Flow<Boolean> { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun getFavoritePagingBooks(): Flow<PagingData<Book>> { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun searchBooksPaging(query: String, sort: String): Flow<PagingData<Book>> { | ||
TODO("Not yet implemented") | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/test/java/com/qualitybitz/booksearchapp/ui/viewmodel/BookViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.qualitybitz.booksearchapp.ui.viewmodel | ||
|
||
import androidx.test.filters.MediumTest | ||
import com.google.common.truth.Truth | ||
import com.qualitybitz.booksearchapp.data.model.Book | ||
import com.qualitybitz.booksearchapp.data.repository.FakeBookSearchRepository | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
@ExperimentalCoroutinesApi | ||
@MediumTest | ||
class BookViewModelTest { | ||
|
||
private lateinit var viewModel: BookViewModel | ||
|
||
@Before | ||
fun setUp() { | ||
viewModel = BookViewModel(FakeBookSearchRepository()) | ||
} | ||
|
||
@Test | ||
fun save_book_test() = runTest { | ||
val book = Book( | ||
listOf("a"), "b", "c", "d", 0, "e", | ||
0, "f", "g", "h", listOf("i"), "j" | ||
) | ||
viewModel.saveBook(book) | ||
|
||
val favoriteBooks = viewModel.favoriteBooks.first() | ||
Truth.assertThat(favoriteBooks).contains(book) | ||
} | ||
} |