Skip to content

Setup unit testing for core/network module [MW-208] #1848

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

Open
wants to merge 1 commit into
base: development
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
7 changes: 6 additions & 1 deletion core/network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,9 @@ dependencies {
add("kspIosX64", libs.ktorfit.ksp)
add("kspIosArm64", libs.ktorfit.ksp)
add("kspIosSimulatorArm64", libs.ktorfit.ksp)
}
testImplementation ("org.jetbrains.kotlin:kotlin-test:1.8.21")
testImplementation ("org.jetbrains.kotlin:kotlin-test-junit:1.8.21")
testImplementation ("io.mockk:mockk:1.13.4") // Mocking library
testImplementation ("androidx.test.ext:junit:1.1.5") // JUnit for Android
Copy link
Contributor

@HekmatullahAmin HekmatullahAmin Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@meetjain6091 I noticed that the test dependencies are currently in the dependencies {} block, but since this is a Kotlin Multiplatform (KMP) project, it would be better to place them inside the kotlin {} block under commonTest and androidTest.

like this:

kotlin {

sourceSets {
    commonTest.dependencies {
        implementation("org.jetbrains.kotlin:kotlin-test:1.8.21")
        implementation("io.mockk:mockk:1.13.4")  // Mocking library
    }

    androidTest.dependencies {
        implementation("androidx.test.ext:junit:1.1.5")
        implementation("androidx.test:core:1.4.0")
    }
}

}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay will surely update it

testImplementation ("androidx.test:core:1.4.0") // For Android testing support
}
36 changes: 36 additions & 0 deletions core/network/src/test/kotlin/com/example/NetworkClientTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Test

// Example class (Replace this with the actual NetworkClient class you're testing)
class NetworkClient {
fun fetchData(url: String): String {
// Normally, this would make a network request
return "Real Response"
}
}

// Unit Test for NetworkClient
class NetworkClientTest {

@Test
fun `test fetchData returns expected response`() = runBlocking {
// Arrange: Create a mock of NetworkClient
val mockClient = mockk<NetworkClient>()

// Mock behavior
every { mockClient.fetchData("https://example.com") } returns "Mocked Response"

// Act: Call the method
val response = mockClient.fetchData("https://example.com")

// Assert: Verify the response
assertEquals("Mocked Response", response)

// Verify that fetchData was called once
verify(exactly = 1) { mockClient.fetchData("https://example.com") }
}
}
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ jbSavedState = "1.2.2"
packageName = "MifosWallet"
packageNamespace = "org.mifospay.desktop"
packageVersion = "1.0.0"
lifecycleRuntimeKtx = "2.8.7"

[libraries]
accompanist-pager = { group = "com.google.accompanist", name = "accompanist-pager", version.ref = "accompanist" }
Expand Down Expand Up @@ -312,6 +313,8 @@ moko-permission = { group = "dev.icerock.moko", name = "permissions", version.re
moko-permission-compose = { group = "dev.icerock.moko", name = "permissions-compose", version.ref = "mokoPermission" }

window-size = { group = "dev.chrisbanes.material3", name = "material3-window-size-class-multiplatform", version.ref = "windowsSizeClass" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you add these?

androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }

[bundles]
androidx-compose-ui-test = [
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ include(":feature:upi-setup")
include(":feature:qr")

include(":libs:mifos-passcode")
include(":core:network:networkclienttest")
Loading