diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 77657ad..863e1c6 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -70,6 +70,7 @@ roboelectric = "org.robolectric:robolectric:4.11.1" dagger-hilt = {module = "com.google.dagger:hilt-android", version.ref = "hilt"} hilt-compiler = {module = "com.google.dagger:hilt-android-compiler", version.ref = "hilt"} hilt-navigation = {module = "androidx.hilt:hilt-navigation-compose", version.ref = "hilt-compose"} +hilt-test = {group ="com.google.dagger", name = "hilt-android-testing", version.ref ="hilt"} # Lifecycle diff --git a/presentation/build.gradle.kts b/presentation/build.gradle.kts index 7e81b02..cc15045 100644 --- a/presentation/build.gradle.kts +++ b/presentation/build.gradle.kts @@ -87,7 +87,6 @@ dependencies { // Navigation implementation(libs.compose.navigation) - androidTestImplementation(libs.compose.navigation.testing) // Roboelectric testImplementation(libs.junit) @@ -98,13 +97,6 @@ dependencies { // Coil-Compose implementation(libs.coil.compose) - // Unit Test - testImplementation(libs.junit) - androidTestImplementation(libs.expresso.core) - testImplementation(libs.mockk) - androidTestImplementation(libs.mockk.android) - testImplementation(libs.mockk.agent) - // Paging implementation(libs.paging.runtime) @@ -126,9 +118,21 @@ dependencies { kapt(libs.hilt.compiler) // Android Test - androidTestImplementation(libs.junit.ext) + debugImplementation(libs.compose.ui.test.manifest) + // Unit Test + testImplementation(libs.junit) + testImplementation(libs.mockk) + testImplementation(libs.mockk.agent) + testImplementation(libs.hilt.test) + + + androidTestImplementation(libs.mockk.android) + androidTestImplementation(libs.junit.ext) + androidTestImplementation(libs.expresso.core) + androidTestImplementation(libs.compose.navigation.testing) + } diff --git a/presentation/src/main/java/com/alexmumo/presentation/bookmarks/BookMarkViewModel.kt b/presentation/src/main/java/com/alexmumo/presentation/bookmarks/BookMarkViewModel.kt index e204a80..d189bd1 100644 --- a/presentation/src/main/java/com/alexmumo/presentation/bookmarks/BookMarkViewModel.kt +++ b/presentation/src/main/java/com/alexmumo/presentation/bookmarks/BookMarkViewModel.kt @@ -21,7 +21,6 @@ import androidx.lifecycle.viewModelScope import com.alexmumo.database.entity.BookMarkEntity import com.alexmumo.domain.repository.BookMarkRepository import dagger.hilt.android.lifecycle.HiltViewModel -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import javax.inject.Inject @@ -29,7 +28,7 @@ import javax.inject.Inject class BookMarkViewModel @Inject constructor(private val bookMarkRepository: BookMarkRepository) : ViewModel() { val bookMarkedNews = bookMarkRepository.getBookMarks() fun saveBookMark(bookMarkEntity: BookMarkEntity) { - viewModelScope.launch(Dispatchers.IO) { + viewModelScope.launch { bookMarkRepository.saveBookMark(bookMarkEntity) } } diff --git a/presentation/src/test/java/com/alexmumo/presentation/bookmarks/BookMarkViewModelTest.kt b/presentation/src/test/java/com/alexmumo/presentation/bookmarks/BookMarkViewModelTest.kt new file mode 100644 index 0000000..3337c21 --- /dev/null +++ b/presentation/src/test/java/com/alexmumo/presentation/bookmarks/BookMarkViewModelTest.kt @@ -0,0 +1,76 @@ +/* + * Copyright 2024 News-App + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alexmumo.presentation.bookmarks + +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.liveData +import com.alexmumo.database.entity.BookMarkEntity +import com.alexmumo.database.entity.SourceEntity +import com.alexmumo.domain.repository.BookMarkRepository +import io.mockk.coEvery +import io.mockk.coVerify +import io.mockk.mockk +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.test.runTest +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +@RunWith(RobolectricTestRunner::class) +class BookMarkViewModelTest { + private val bookMarkRepository = mockk(relaxed = true) + private lateinit var bookMarkViewModel: BookMarkViewModel + + @Before + fun setUp() { + bookMarkViewModel = BookMarkViewModel(bookMarkRepository) + } + + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun `test saveBookMark returns success`() = runTest { + coEvery { bookMarkRepository.getBookMarks() } returns flowOf(listOf(bookMark)) + bookMarkViewModel.saveBookMark(bookMark) + coVerify { bookMarkViewModel.saveBookMark(bookMark) } + } + + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun `test delete returns success`() = runTest { + bookMarkViewModel.deleteBookMarkedNews() + coVerify { bookMarkViewModel.deleteBookMarkedNews() } + } + + @Test + fun `test checkBookMarked returns true`() = runTest { + val bookMarkId = "1" + val liveData = MutableLiveData() + coEvery { bookMarkViewModel.checkBookMarked(id = "1") } returns liveData + val expectedValue = bookMarkViewModel.checkBookMarked(id = bookMarkId) + expectedValue.observeForever { value -> + Assert.assertEquals(expectedValue, value) + } + } + + companion object { + val bookMark = BookMarkEntity( + author = null, content = null, description = null, publishedAt = null, SourceEntity(id = null, name = ""), title = null, url = "", urlToImage = null + ) + } +}