Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests #23

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 13 additions & 9 deletions presentation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ dependencies {

// Navigation
implementation(libs.compose.navigation)
androidTestImplementation(libs.compose.navigation.testing)

// Roboelectric
testImplementation(libs.junit)
Expand All @@ -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)
Expand All @@ -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)

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ 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

@HiltViewModel
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)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<BookMarkRepository>(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<Boolean>()
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
)
}
}
Loading