Migrate from JUnit assertions to Kotlin's test framework

Fixes #419
pull/420/head
Simon Marquis 2 years ago
parent 11950859d3
commit 90dc300783

@ -98,6 +98,7 @@ dependencies {
androidTestImplementation(project(":core:data-test"))
androidTestImplementation(project(":core:network"))
androidTestImplementation(libs.androidx.navigation.testing)
androidTestImplementation(kotlin("test"))
debugImplementation(libs.androidx.compose.ui.testManifest)
implementation(libs.accompanist.systemuicontroller)

@ -31,13 +31,13 @@ import androidx.navigation.compose.composable
import androidx.navigation.createGraph
import androidx.navigation.testing.TestNavHostController
import com.google.samples.apps.nowinandroid.core.testing.util.TestNetworkMonitor
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test

@ -21,6 +21,7 @@ import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.kotlin
class AndroidFeatureConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
@ -46,7 +47,9 @@ class AndroidFeatureConventionPlugin : Plugin<Project> {
add("implementation", project(":core:common"))
add("implementation", project(":core:domain"))
add("testImplementation", kotlin("test"))
add("testImplementation", project(":core:testing"))
add("androidTestImplementation", kotlin("test"))
add("androidTestImplementation", project(":core:testing"))
add("implementation", libs.findLibrary("coil.kt").get())

@ -26,6 +26,7 @@ import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.kotlin
class AndroidLibraryConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
@ -51,6 +52,10 @@ class AndroidLibraryConventionPlugin : Plugin<Project> {
force("org.objenesis:objenesis:2.6")
}
}
dependencies {
add("androidTestImplementation", kotlin("test"))
add("testImplementation", kotlin("test"))
}
}
}
}

@ -17,9 +17,9 @@
package com.google.samples.apps.nowinandroid.core.result
import app.cash.turbine.test
import kotlin.test.assertEquals
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Test
class ResultKtTest {

@ -21,8 +21,8 @@ import com.google.samples.apps.nowinandroid.core.network.model.NetworkAuthor
import com.google.samples.apps.nowinandroid.core.network.model.NetworkNewsResource
import com.google.samples.apps.nowinandroid.core.network.model.NetworkNewsResourceExpanded
import com.google.samples.apps.nowinandroid.core.network.model.NetworkTopic
import kotlin.test.assertEquals
import kotlinx.datetime.Instant
import org.junit.Assert.assertEquals
import org.junit.Test
class NetworkEntityKtTest {

@ -29,9 +29,9 @@ import com.google.samples.apps.nowinandroid.core.datastore.test.testUserPreferen
import com.google.samples.apps.nowinandroid.core.model.data.Author
import com.google.samples.apps.nowinandroid.core.network.model.NetworkAuthor
import com.google.samples.apps.nowinandroid.core.network.model.NetworkChangeList
import kotlin.test.assertEquals
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test
@ -68,7 +68,7 @@ class OfflineFirstAuthorsRepositoryTest {
@Test
fun offlineFirstAuthorsRepository_Authors_stream_is_backed_by_Authors_dao() =
runTest {
Assert.assertEquals(
assertEquals(
authorDao.getAuthorEntitiesStream()
.first()
.map(AuthorEntity::asExternalModel),
@ -88,13 +88,13 @@ class OfflineFirstAuthorsRepositoryTest {
val dbAuthors = authorDao.getAuthorEntitiesStream()
.first()
Assert.assertEquals(
assertEquals(
networkAuthors.map(AuthorEntity::id),
dbAuthors.map(AuthorEntity::id)
)
// After sync version should be updated
Assert.assertEquals(
assertEquals(
network.latestChangeListVersion(CollectionType.Authors),
synchronizer.getChangeListVersions().authorVersion
)
@ -125,13 +125,13 @@ class OfflineFirstAuthorsRepositoryTest {
val db = authorDao.getAuthorEntitiesStream()
.first()
Assert.assertEquals(
assertEquals(
network.map(AuthorEntity::id),
db.map(AuthorEntity::id)
)
// After sync version should be updated
Assert.assertEquals(
assertEquals(
changeList.last().changeListVersion,
synchronizer.getChangeListVersions().authorVersion
)
@ -166,13 +166,13 @@ class OfflineFirstAuthorsRepositoryTest {
.map(AuthorEntity::asExternalModel)
// Assert that items marked deleted on the network have been deleted locally
Assert.assertEquals(
assertEquals(
networkAuthors.map(Author::id) - deletedItems,
dbAuthors.map(Author::id)
)
// After sync version should be updated
Assert.assertEquals(
assertEquals(
network.latestChangeListVersion(CollectionType.Authors),
synchronizer.getChangeListVersions().authorVersion
)

@ -39,9 +39,9 @@ import com.google.samples.apps.nowinandroid.core.datastore.test.testUserPreferen
import com.google.samples.apps.nowinandroid.core.model.data.NewsResource
import com.google.samples.apps.nowinandroid.core.network.model.NetworkChangeList
import com.google.samples.apps.nowinandroid.core.network.model.NetworkNewsResource
import kotlin.test.assertEquals
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
@ -112,7 +112,7 @@ class OfflineFirstNewsRepositoryTest {
)
assertEquals(
emptyList<NewsResource>(),
emptyList(),
subject.getNewsResourcesStream(
filterTopicIds = nonPresentInterestsIds,
)
@ -136,7 +136,7 @@ class OfflineFirstNewsRepositoryTest {
)
assertEquals(
emptyList<NewsResource>(),
emptyList(),
subject.getNewsResourcesStream(
filterAuthorIds = nonPresentInterestsIds
)

@ -28,9 +28,9 @@ import com.google.samples.apps.nowinandroid.core.datastore.NiaPreferencesDataSou
import com.google.samples.apps.nowinandroid.core.datastore.test.testUserPreferencesDataStore
import com.google.samples.apps.nowinandroid.core.model.data.Topic
import com.google.samples.apps.nowinandroid.core.network.model.NetworkTopic
import kotlin.test.assertEquals
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test
@ -69,7 +69,7 @@ class OfflineFirstTopicsRepositoryTest {
@Test
fun offlineFirstTopicsRepository_topics_stream_is_backed_by_topics_dao() =
runTest {
Assert.assertEquals(
assertEquals(
topicDao.getTopicEntitiesStream()
.first()
.map(TopicEntity::asExternalModel),
@ -89,13 +89,13 @@ class OfflineFirstTopicsRepositoryTest {
val dbTopics = topicDao.getTopicEntitiesStream()
.first()
Assert.assertEquals(
assertEquals(
networkTopics.map(TopicEntity::id),
dbTopics.map(TopicEntity::id)
)
// After sync version should be updated
Assert.assertEquals(
assertEquals(
network.latestChangeListVersion(CollectionType.Topics),
synchronizer.getChangeListVersions().topicVersion
)
@ -119,13 +119,13 @@ class OfflineFirstTopicsRepositoryTest {
val dbTopics = topicDao.getTopicEntitiesStream()
.first()
Assert.assertEquals(
assertEquals(
networkTopics.map(TopicEntity::id),
dbTopics.map(TopicEntity::id)
)
// After sync version should be updated
Assert.assertEquals(
assertEquals(
network.latestChangeListVersion(CollectionType.Topics),
synchronizer.getChangeListVersions().topicVersion
)
@ -160,13 +160,13 @@ class OfflineFirstTopicsRepositoryTest {
.map(TopicEntity::asExternalModel)
// Assert that items marked deleted on the network have been deleted locally
Assert.assertEquals(
assertEquals(
networkTopics.map(Topic::id) - deletedItems,
dbTopics.map(Topic::id)
)
// After sync version should be updated
Assert.assertEquals(
assertEquals(
network.latestChangeListVersion(CollectionType.Topics),
synchronizer.getChangeListVersions().topicVersion
)

@ -21,10 +21,12 @@ import com.google.samples.apps.nowinandroid.core.datastore.test.testUserPreferen
import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig
import com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand
import com.google.samples.apps.nowinandroid.core.model.data.UserData
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
@ -194,9 +196,9 @@ class OfflineFirstUserDataRepositoryTest {
runTest {
subject.setFollowedTopicIds(setOf("1"))
subject.setShouldHideOnboarding(true)
assertEquals(true, subject.userDataStream.first().shouldHideOnboarding)
assertTrue(subject.userDataStream.first().shouldHideOnboarding)
subject.setFollowedTopicIds(emptySet())
assertEquals(false, subject.userDataStream.first().shouldHideOnboarding)
assertFalse(subject.userDataStream.first().shouldHideOnboarding)
}
}

@ -20,8 +20,8 @@ import com.google.samples.apps.nowinandroid.core.model.data.Author
import com.google.samples.apps.nowinandroid.core.model.data.NewsResource
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video
import com.google.samples.apps.nowinandroid.core.model.data.Topic
import kotlin.test.assertEquals
import kotlinx.datetime.Instant
import org.junit.Assert.assertEquals
import org.junit.Test
class PopulatedNewsResourceKtTest {

@ -17,7 +17,7 @@
package com.google.samples.apps.nowinandroid.core.database.util
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType
import org.junit.Assert.assertEquals
import kotlin.test.assertEquals
import org.junit.Test
class NewsResourceTypeConverterTest {

@ -27,10 +27,10 @@ import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceTopi
import com.google.samples.apps.nowinandroid.core.database.model.TopicEntity
import com.google.samples.apps.nowinandroid.core.database.model.asExternalModel
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType
import kotlin.test.assertEquals
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.Instant
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test

@ -16,9 +16,9 @@
package com.google.samples.apps.nowinandroid.core.datastore
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
/**

@ -16,8 +16,9 @@
package com.google.samples.apps.nowinandroid.core.datastore
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlinx.coroutines.test.runTest
import org.junit.Assert
import org.junit.Test
class ListToMapMigrationTest {
@ -29,7 +30,7 @@ class ListToMapMigrationTest {
deprecatedFollowedTopicIds.addAll(listOf("1", "2", "3"))
}
// Assert that there are no topic ids in the map yet
Assert.assertEquals(
assertEquals(
emptyMap<String, Boolean>(),
preMigrationUserPreferences.followedTopicIdsMap
)
@ -39,13 +40,13 @@ class ListToMapMigrationTest {
ListToMapMigration.migrate(preMigrationUserPreferences)
// Assert the deprecated topic ids have been migrated to the topic ids map
Assert.assertEquals(
assertEquals(
mapOf("1" to true, "2" to true, "3" to true),
postMigrationUserPreferences.followedTopicIdsMap
)
// Assert that the migration has been marked complete
Assert.assertTrue(postMigrationUserPreferences.hasDoneListToMapMigration)
assertTrue(postMigrationUserPreferences.hasDoneListToMapMigration)
}
@Test
@ -55,7 +56,7 @@ class ListToMapMigrationTest {
deprecatedFollowedAuthorIds.addAll(listOf("4", "5", "6"))
}
// Assert that there are no author ids in the map yet
Assert.assertEquals(
assertEquals(
emptyMap<String, Boolean>(),
preMigrationUserPreferences.followedAuthorIdsMap
)
@ -65,13 +66,13 @@ class ListToMapMigrationTest {
ListToMapMigration.migrate(preMigrationUserPreferences)
// Assert the deprecated author ids have been migrated to the author ids map
Assert.assertEquals(
assertEquals(
mapOf("4" to true, "5" to true, "6" to true),
postMigrationUserPreferences.followedAuthorIdsMap
)
// Assert that the migration has been marked complete
Assert.assertTrue(postMigrationUserPreferences.hasDoneListToMapMigration)
assertTrue(postMigrationUserPreferences.hasDoneListToMapMigration)
}
@Test
@ -81,7 +82,7 @@ class ListToMapMigrationTest {
deprecatedBookmarkedNewsResourceIds.addAll(listOf("7", "8", "9"))
}
// Assert that there are no bookmarks in the map yet
Assert.assertEquals(
assertEquals(
emptyMap<String, Boolean>(),
preMigrationUserPreferences.bookmarkedNewsResourceIdsMap
)
@ -91,12 +92,12 @@ class ListToMapMigrationTest {
ListToMapMigration.migrate(preMigrationUserPreferences)
// Assert the deprecated bookmarks have been migrated to the bookmarks map
Assert.assertEquals(
assertEquals(
mapOf("7" to true, "8" to true, "9" to true),
postMigrationUserPreferences.bookmarkedNewsResourceIdsMap
)
// Assert that the migration has been marked complete
Assert.assertTrue(postMigrationUserPreferences.hasDoneListToMapMigration)
assertTrue(postMigrationUserPreferences.hasDoneListToMapMigration)
}
}

@ -17,9 +17,10 @@
package com.google.samples.apps.nowinandroid.core.datastore
import com.google.samples.apps.nowinandroid.core.datastore.test.testUserPreferencesDataStore
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
@ -40,13 +41,13 @@ class NiaPreferencesDataSourceTest {
@Test
fun shouldHideOnboardingIsFalseByDefault() = runTest {
assertEquals(false, subject.userDataStream.first().shouldHideOnboarding)
assertFalse(subject.userDataStream.first().shouldHideOnboarding)
}
@Test
fun userShouldHideOnboardingIsTrueWhenSet() = runTest {
subject.setShouldHideOnboarding(true)
assertEquals(true, subject.userDataStream.first().shouldHideOnboarding)
assertTrue(subject.userDataStream.first().shouldHideOnboarding)
}
@Test
@ -60,7 +61,7 @@ class NiaPreferencesDataSourceTest {
subject.toggleFollowedAuthorId("1", false)
// Then: onboarding should be shown again
assertEquals(false, subject.userDataStream.first().shouldHideOnboarding)
assertFalse(subject.userDataStream.first().shouldHideOnboarding)
}
@Test
@ -74,7 +75,7 @@ class NiaPreferencesDataSourceTest {
subject.toggleFollowedTopicId("1", false)
// Then: onboarding should be shown again
assertEquals(false, subject.userDataStream.first().shouldHideOnboarding)
assertFalse(subject.userDataStream.first().shouldHideOnboarding)
}
@Test
@ -88,7 +89,7 @@ class NiaPreferencesDataSourceTest {
subject.setFollowedAuthorIds(emptySet())
// Then: onboarding should be shown again
assertEquals(false, subject.userDataStream.first().shouldHideOnboarding)
assertFalse(subject.userDataStream.first().shouldHideOnboarding)
}
@Test
@ -102,7 +103,7 @@ class NiaPreferencesDataSourceTest {
subject.setFollowedTopicIds(emptySet())
// Then: onboarding should be shown again
assertEquals(false, subject.userDataStream.first().shouldHideOnboarding)
assertFalse(subject.userDataStream.first().shouldHideOnboarding)
}
@Test
@ -117,7 +118,7 @@ class NiaPreferencesDataSourceTest {
subject.setFollowedTopicIds(emptySet())
// Then: onboarding should still be dismissed
assertEquals(true, subject.userDataStream.first().shouldHideOnboarding)
assertTrue(subject.userDataStream.first().shouldHideOnboarding)
}
@Test
@ -132,6 +133,6 @@ class NiaPreferencesDataSourceTest {
subject.setFollowedAuthorIds(emptySet())
// Then: onboarding should still be dismissed
assertEquals(true, subject.userDataStream.first().shouldHideOnboarding)
assertTrue(subject.userDataStream.first().shouldHideOnboarding)
}
}

@ -19,8 +19,8 @@ package com.google.samples.apps.nowinandroid.core.datastore
import androidx.datastore.core.CorruptionException
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import kotlin.test.assertEquals
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Test
class UserPreferencesSerializerTest {

@ -36,7 +36,7 @@ import com.google.samples.apps.nowinandroid.core.designsystem.theme.LightDefault
import com.google.samples.apps.nowinandroid.core.designsystem.theme.LocalBackgroundTheme
import com.google.samples.apps.nowinandroid.core.designsystem.theme.LocalGradientColors
import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
import org.junit.Assert.assertEquals
import kotlin.test.assertEquals
import org.junit.Rule
import org.junit.Test

@ -22,9 +22,9 @@ import com.google.samples.apps.nowinandroid.core.model.data.Topic
import com.google.samples.apps.nowinandroid.core.testing.repository.TestTopicsRepository
import com.google.samples.apps.nowinandroid.core.testing.repository.TestUserDataRepository
import com.google.samples.apps.nowinandroid.core.testing.util.MainDispatcherRule
import kotlin.test.assertEquals
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test

@ -24,10 +24,10 @@ import com.google.samples.apps.nowinandroid.core.model.data.Topic
import com.google.samples.apps.nowinandroid.core.testing.repository.TestNewsRepository
import com.google.samples.apps.nowinandroid.core.testing.repository.TestUserDataRepository
import com.google.samples.apps.nowinandroid.core.testing.util.MainDispatcherRule
import kotlin.test.assertEquals
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.Instant
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test

@ -21,9 +21,9 @@ import com.google.samples.apps.nowinandroid.core.model.data.Author
import com.google.samples.apps.nowinandroid.core.testing.repository.TestAuthorsRepository
import com.google.samples.apps.nowinandroid.core.testing.repository.TestUserDataRepository
import com.google.samples.apps.nowinandroid.core.testing.util.MainDispatcherRule
import kotlin.test.assertEquals
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test

@ -16,10 +16,10 @@
package com.google.samples.apps.nowinandroid.core.network.fake
import kotlin.test.assertEquals
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.json.Json
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test

@ -17,8 +17,8 @@
package com.google.samples.apps.nowinandroid.core.network.model.util
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType
import kotlin.test.assertEquals
import kotlinx.serialization.json.Json
import org.junit.Assert.assertEquals
import org.junit.Test
class NewsResourceTypeSerializerTest {

@ -35,11 +35,12 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.Instant
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
import kotlin.test.assertTrue
/**
* To learn more about how this test handles Flows created with stateIn, see
@ -83,15 +84,14 @@ class AuthorViewModelTest {
userDataRepository.setFollowedAuthorIds(setOf(testInputAuthors[1].author.id))
val item = viewModel.authorUiState.value
assertTrue(item is AuthorUiState.Success)
assertIs<AuthorUiState.Success>(item)
val successAuthorUiState = item as AuthorUiState.Success
val authorFromRepository = authorsRepository.getAuthorStream(
id = testInputAuthors[0].author.id
).first()
successAuthorUiState.followableAuthor.author
assertEquals(authorFromRepository, successAuthorUiState.followableAuthor.author)
item.followableAuthor.author
assertEquals(authorFromRepository, item.followableAuthor.author)
collectJob.cancel()
}
@ -132,8 +132,8 @@ class AuthorViewModelTest {
val authorState = viewModel.authorUiState.value
val newsUiState = viewModel.newsUiState.value
assertTrue(authorState is AuthorUiState.Success)
assertTrue(newsUiState is NewsUiState.Loading)
assertIs<AuthorUiState.Success>(authorState)
assertIs<NewsUiState.Loading>(newsUiState)
collectJob.cancel()
}
@ -155,8 +155,8 @@ class AuthorViewModelTest {
val authorState = viewModel.authorUiState.value
val newsUiState = viewModel.newsUiState.value
assertTrue(authorState is AuthorUiState.Success)
assertTrue(newsUiState is NewsUiState.Success)
assertIs<AuthorUiState.Success>(authorState)
assertIs<NewsUiState.Success>(newsUiState)
collectJob.cancel()
}

@ -34,8 +34,8 @@ import androidx.compose.ui.test.performScrollToNode
import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource
import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources
import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import org.junit.Rule
import org.junit.Test

@ -27,11 +27,11 @@ import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
/**
* To learn more about how this test handles Flows created with stateIn, see
@ -69,8 +69,8 @@ class BookmarksViewModelTest {
newsRepository.sendNewsResources(previewNewsResources)
userDataRepository.updateNewsResourceBookmark(previewNewsResources[0].id, true)
val item = viewModel.feedUiState.value
assertTrue(item is Success)
assertEquals((item as Success).feed.size, 1)
assertIs<Success>(item)
assertEquals(item.feed.size, 1)
collectJob.cancel()
}
@ -86,8 +86,8 @@ class BookmarksViewModelTest {
viewModel.removeFromSavedResources(previewNewsResources[0].id)
// Verify list of saved resources is now empty
val item = viewModel.feedUiState.value
assertTrue(item is Success)
assertEquals((item as Success).feed.size, 0)
assertIs<Success>(item)
assertEquals(item.feed.size, 0)
collectJob.cancel()
}

@ -40,10 +40,10 @@ import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.Instant
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import kotlin.test.assertEquals
/**
* To learn more about how this test handles Flows created with stateIn, see

@ -28,11 +28,11 @@ import com.google.samples.apps.nowinandroid.core.testing.repository.TestUserData
import com.google.samples.apps.nowinandroid.core.testing.util.MainDispatcherRule
import com.google.samples.apps.nowinandroid.feature.interests.InterestsUiState
import com.google.samples.apps.nowinandroid.feature.interests.InterestsViewModel
import kotlin.test.assertEquals
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test

@ -22,7 +22,7 @@ import com.google.samples.apps.nowinandroid.core.testing.repository.TestUserData
import com.google.samples.apps.nowinandroid.core.testing.util.MainDispatcherRule
import com.google.samples.apps.nowinandroid.feature.settings.SettingsUiState.Loading
import com.google.samples.apps.nowinandroid.feature.settings.SettingsUiState.Success
import junit.framework.Assert.assertEquals
import kotlin.test.assertEquals
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.UnconfinedTestDispatcher

@ -35,11 +35,11 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.Instant
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
/**
* To learn more about how this test handles Flows created with stateIn, see
@ -77,14 +77,13 @@ class TopicViewModelTest {
topicsRepository.sendTopics(testInputTopics.map(FollowableTopic::topic))
userDataRepository.setFollowedTopicIds(setOf(testInputTopics[1].topic.id))
val item = viewModel.topicUiState.value
assertTrue(item is TopicUiState.Success)
assertIs<TopicUiState.Success>(item)
val successTopicState = item as TopicUiState.Success
val topicFromRepository = topicsRepository.getTopic(
testInputTopics[0].topic.id
).first()
assertEquals(topicFromRepository, successTopicState.followableTopic.topic)
assertEquals(topicFromRepository, item.followableTopic.topic)
collectJob.cancel()
}
@ -119,8 +118,8 @@ class TopicViewModelTest {
val topicUiState = viewModel.topicUiState.value
val newsUiState = viewModel.newUiState.value
assertTrue(topicUiState is TopicUiState.Success)
assertTrue(newsUiState is NewsUiState.Loading)
assertIs<TopicUiState.Success>(topicUiState)
assertIs<NewsUiState.Loading>(newsUiState)
collectJob.cancel()
}
@ -141,8 +140,8 @@ class TopicViewModelTest {
val topicUiState = viewModel.topicUiState.value
val newsUiState = viewModel.newUiState.value
assertTrue(topicUiState is TopicUiState.Success)
assertTrue(newsUiState is NewsUiState.Success)
assertIs<TopicUiState.Success>(topicUiState)
assertIs<NewsUiState.Success>(newsUiState)
collectJob.cancel()
}

@ -25,7 +25,7 @@ import androidx.work.testing.SynchronousExecutor
import androidx.work.testing.WorkManagerTestInitHelper
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.Assert.assertEquals
import kotlin.test.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test

Loading…
Cancel
Save