Skip to content

Commit

Permalink
refactor #605 Modify tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
toastkidjp committed Jun 4, 2023
1 parent f041294 commit c1edf10
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import io.mockk.verify
import jp.toastkid.data.DatabaseFinder
import jp.toastkid.data.repository.factory.RepositoryFactory
import jp.toastkid.lib.storage.FilesDir
import jp.toastkid.yobidashi.browser.FaviconFolderProviderService
import jp.toastkid.yobidashi.browser.bookmark.model.BookmarkRepository
import jp.toastkid.yobidashi.browser.icon.WebClipIconLoader
import jp.toastkid.yobidashi.libs.db.AppDatabase
import kotlinx.coroutines.Dispatchers
import org.junit.After
import org.junit.Before
Expand All @@ -45,12 +44,6 @@ class BookmarkInitializerTest {
@MockK
private lateinit var webClipIconLoader: WebClipIconLoader

@MockK
private lateinit var databaseFinder: DatabaseFinder

@MockK
private lateinit var appDatabase: AppDatabase

@MockK
private lateinit var bookmarkRepository: BookmarkRepository

Expand All @@ -64,8 +57,7 @@ class BookmarkInitializerTest {
fun setUp() {
MockKAnnotations.init(this)

every { databaseFinder.invoke(any()) }.returns(appDatabase)
every { appDatabase.bookmarkRepository() }.returns(bookmarkRepository)
every { anyConstructed<RepositoryFactory>().bookmarkRepository(context) }.returns(bookmarkRepository)
coEvery { bookmarkRepository.add(any()) }.just(Runs)
coEvery { onComplete.invoke() }.just(Runs)

Expand All @@ -90,16 +82,15 @@ class BookmarkInitializerTest {

@Test
fun testFrom() {
mockkConstructor(FaviconFolderProviderService::class, DatabaseFinder::class)
mockkConstructor(FaviconFolderProviderService::class, RepositoryFactory::class)
every { anyConstructed<FaviconFolderProviderService>().invoke(any()) }.returns(mockk())
every { anyConstructed<DatabaseFinder>().invoke(any()) }.returns(appDatabase)
every { anyConstructed<RepositoryFactory>().bookmarkRepository(any()) }.returns(bookmarkRepository)
mockkObject(WebClipIconLoader)
every { WebClipIconLoader.from(any()) }.returns(mockk())

BookmarkInitializer.from(context)

verify(exactly = 1) { anyConstructed<DatabaseFinder>().invoke(any()) }
verify(exactly = 1) { appDatabase.bookmarkRepository() }
verify(exactly = 1) { anyConstructed<RepositoryFactory>().bookmarkRepository(any()) }
verify { anyConstructed<FaviconFolderProviderService>().invoke(any()) }
verify { WebClipIconLoader.from(any()) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import io.mockk.coVerify
import io.mockk.impl.annotations.MockK
import io.mockk.mockkConstructor
import io.mockk.unmockkAll
import jp.toastkid.data.DatabaseFinder
import jp.toastkid.data.repository.factory.RepositoryFactory
import jp.toastkid.yobidashi.browser.bookmark.model.BookmarkRepository
import jp.toastkid.yobidashi.libs.db.AppDatabase
import kotlinx.coroutines.Dispatchers
import org.junit.After
import org.junit.Before
Expand All @@ -33,19 +32,15 @@ class BookmarkInsertionTest {
@MockK
private lateinit var context: Context

@MockK
private lateinit var appDatabase: AppDatabase

@MockK
private lateinit var bookmarkRepository: BookmarkRepository

@Before
fun setUp() {
MockKAnnotations.init(this)

mockkConstructor(DatabaseFinder::class)
coEvery { anyConstructed<DatabaseFinder>().invoke(any()) }.returns(appDatabase)
coEvery { appDatabase.bookmarkRepository() }.returns(bookmarkRepository)
mockkConstructor(RepositoryFactory::class)
coEvery { anyConstructed<RepositoryFactory>().bookmarkRepository(any()) }.returns(bookmarkRepository)
coEvery { bookmarkRepository.add(any()) }.returns(Unit)
}

Expand All @@ -60,8 +55,7 @@ class BookmarkInsertionTest {

bookmarkInsertion.insert()

coVerify(exactly = 0) { anyConstructed<DatabaseFinder>().invoke(any()) }
coVerify(exactly = 0) { appDatabase.bookmarkRepository() }
coVerify(exactly = 0) { anyConstructed<RepositoryFactory>().bookmarkRepository(any()) }
coVerify(exactly = 0) { bookmarkRepository.add(any()) }
}

Expand All @@ -76,8 +70,7 @@ class BookmarkInsertionTest {

bookmarkInsertion.insert()

coVerify(exactly = 1) { anyConstructed<DatabaseFinder>().invoke(any()) }
coVerify(exactly = 1) { appDatabase.bookmarkRepository() }
coVerify(exactly = 1) { anyConstructed<RepositoryFactory>().bookmarkRepository(any()) }
coVerify(exactly = 1) { bookmarkRepository.add(any()) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import io.mockk.just
import io.mockk.mockk
import io.mockk.mockkConstructor
import io.mockk.unmockkAll
import jp.toastkid.data.DatabaseFinder
import jp.toastkid.yobidashi.libs.db.AppDatabase
import jp.toastkid.data.repository.factory.RepositoryFactory
import kotlinx.coroutines.Dispatchers
import org.junit.After
import org.junit.Before
Expand All @@ -28,20 +27,15 @@ class ViewHistoryInsertionTest {

private lateinit var viewHistoryInsertion: ViewHistoryInsertion

@MockK
private lateinit var appDatabase: AppDatabase

@MockK
private lateinit var repository: ViewHistoryRepository

@Before
fun setUp() {
MockKAnnotations.init(this)

mockkConstructor(DatabaseFinder::class)
coEvery { anyConstructed<DatabaseFinder>().invoke(any()) }.returns(appDatabase)

coEvery { appDatabase.viewHistoryRepository() }.returns(repository)
mockkConstructor(RepositoryFactory::class)
coEvery { anyConstructed<RepositoryFactory>().viewHistoryRepository(any()) }.returns(repository)
coEvery { repository.add(any()) }.just(Runs)
}

Expand All @@ -58,8 +52,7 @@ class ViewHistoryInsertionTest {

viewHistoryInsertion.invoke()

coVerify(exactly = 1) { anyConstructed<DatabaseFinder>().invoke(any()) }
coVerify(exactly = 1) { appDatabase.viewHistoryRepository() }
coVerify(exactly = 1) { anyConstructed<RepositoryFactory>().viewHistoryRepository(any()) }
coVerify(exactly = 1) { repository.add(any()) }
}

Expand All @@ -71,8 +64,7 @@ class ViewHistoryInsertionTest {

viewHistoryInsertion.invoke()

coVerify(exactly = 1) { anyConstructed<DatabaseFinder>().invoke(any()) }
coVerify(exactly = 1) { appDatabase.viewHistoryRepository() }
coVerify(exactly = 1) { anyConstructed<RepositoryFactory>().viewHistoryRepository(any()) }
coVerify(exactly = 0) { repository.add(any()) }
}

Expand All @@ -84,8 +76,7 @@ class ViewHistoryInsertionTest {

viewHistoryInsertion.invoke()

coVerify(exactly = 1) { anyConstructed<DatabaseFinder>().invoke(any()) }
coVerify(exactly = 1) { appDatabase.viewHistoryRepository() }
coVerify(exactly = 1) { anyConstructed<RepositoryFactory>().viewHistoryRepository(any()) }
coVerify(exactly = 0) { repository.add(any()) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import io.mockk.Runs
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.just
import io.mockk.mockk
import io.mockk.mockkConstructor
import io.mockk.unmockkAll
import io.mockk.verify
import jp.toastkid.data.DatabaseFinder
import jp.toastkid.yobidashi.libs.db.AppDatabase
import jp.toastkid.data.repository.factory.RepositoryFactory
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import org.junit.After
Expand All @@ -29,9 +29,6 @@ class FavoriteSearchInsertionTest {

private lateinit var favoriteSearchInsertion: FavoriteSearchInsertion

@MockK
private lateinit var appDatabase: AppDatabase

@MockK
private lateinit var repository: FavoriteSearchRepository

Expand All @@ -48,9 +45,8 @@ class FavoriteSearchInsertionTest {
fun setUp() {
MockKAnnotations.init(this)

mockkConstructor(DatabaseFinder::class)
every { anyConstructed<DatabaseFinder>().invoke(any()) }.returns(appDatabase)
every { appDatabase.favoriteSearchRepository() }.returns(repository)
mockkConstructor(RepositoryFactory::class)
every { anyConstructed<RepositoryFactory>().favoriteSearchRepository(mockk()) }.returns(repository)
every { repository.insert(any()) }.just(Runs)
}

Expand All @@ -71,8 +67,7 @@ class FavoriteSearchInsertionTest {

favoriteSearchInsertion.invoke()

verify(exactly = 1) { anyConstructed<DatabaseFinder>().invoke(any()) }
verify(exactly = 1) { appDatabase.favoriteSearchRepository() }
verify(exactly = 1) { anyConstructed<RepositoryFactory>().favoriteSearchRepository(mockk()) }
verify(exactly = 1) { repository.insert(any()) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import io.mockk.mockk
import io.mockk.mockkConstructor
import io.mockk.unmockkAll
import io.mockk.verify
import jp.toastkid.data.DatabaseFinder
import jp.toastkid.data.repository.factory.RepositoryFactory
import jp.toastkid.yobidashi.libs.db.AppDatabase
import kotlinx.coroutines.Dispatchers
import org.junit.After
Expand All @@ -40,9 +40,8 @@ class SearchHistoryInsertionTest {
fun setUp() {
MockKAnnotations.init(this)

mockkConstructor(DatabaseFinder::class)
every { anyConstructed<DatabaseFinder>().invoke(any()) }.returns(appDatabase)
every { appDatabase.searchHistoryRepository() }.returns(repository)
mockkConstructor(RepositoryFactory::class)
every { anyConstructed<RepositoryFactory>().searchHistoryRepository(any()) }.returns(repository)
coEvery { repository.insert(any()) }.just(Runs)
}

Expand All @@ -62,8 +61,7 @@ class SearchHistoryInsertionTest {

searchHistoryInsertion.insert()

verify(exactly = 1) { anyConstructed<DatabaseFinder>().invoke(any()) }
verify(exactly = 1) { appDatabase.searchHistoryRepository() }
verify(exactly = 1) { anyConstructed<RepositoryFactory>().searchHistoryRepository(any()) }
coVerify(exactly = 1) { repository.insert(any()) }
}

Expand All @@ -78,8 +76,7 @@ class SearchHistoryInsertionTest {

searchHistoryInsertion.insert()

verify(exactly = 1) { anyConstructed<DatabaseFinder>().invoke(any()) }
verify(exactly = 1) { appDatabase.searchHistoryRepository() }
verify(exactly = 1) { anyConstructed<RepositoryFactory>().searchHistoryRepository(any()) }
coVerify(exactly = 0) { repository.insert(any()) }
}

Expand All @@ -94,8 +91,7 @@ class SearchHistoryInsertionTest {

searchHistoryInsertion.insert()

verify(exactly = 1) { anyConstructed<DatabaseFinder>().invoke(any()) }
verify(exactly = 1) { appDatabase.searchHistoryRepository() }
verify(exactly = 1) { anyConstructed<RepositoryFactory>().searchHistoryRepository(any()) }
coVerify(exactly = 0) { repository.insert(any()) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import io.mockk.coVerify
import io.mockk.impl.annotations.InjectMockKs
import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import io.mockk.mockkConstructor
import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import jp.toastkid.data.DatabaseFinder
import jp.toastkid.yobidashi.libs.db.AppDatabase
import jp.toastkid.data.repository.factory.RepositoryFactory
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runBlockingTest
import org.junit.After
Expand All @@ -32,21 +32,15 @@ class DefaultColorInsertionTest {
@InjectMockKs
private lateinit var defaultColorInsertion: DefaultColorInsertion

@MockK
private lateinit var databaseFinder: DatabaseFinder

@MockK
private lateinit var appDatabase: AppDatabase

@MockK
private lateinit var savedColorRepository: SavedColorRepository

@Before
fun setUp() {
MockKAnnotations.init(this)

coEvery { databaseFinder.invoke(any()) }.returns(appDatabase)
coEvery { appDatabase.savedColorRepository() }.returns(savedColorRepository)
mockkConstructor(RepositoryFactory::class)
coEvery { anyConstructed<RepositoryFactory>().savedColorRepository(any()) }.returns(savedColorRepository)
coEvery { savedColorRepository.add(any()) }.answers { 1L }

mockkStatic(ContextCompat::class)
Expand All @@ -67,8 +61,7 @@ class DefaultColorInsertionTest {

delay(1000L)

coVerify (exactly = 1) { databaseFinder.invoke(any()) }
coVerify (exactly = 1) { appDatabase.savedColorRepository() }
coVerify (exactly = 1) { anyConstructed<RepositoryFactory>().savedColorRepository(any()) }
coVerify (atLeast = 1) { savedColorRepository.add(any()) }
coVerify (atLeast = 1) { ContextCompat.getColor(any(), any()) }
coVerify (atLeast = 1) { SavedColor.make(any(), any()) }
Expand Down

0 comments on commit c1edf10

Please sign in to comment.