Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucienshema committed Nov 14, 2024
1 parent f4842df commit 32bb5d7
Show file tree
Hide file tree
Showing 10 changed files with 995 additions and 45 deletions.
6 changes: 0 additions & 6 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 19 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ plugins {
}


def googleMapsApiKey = System.getenv("GOOGLE_MAPS_API_KEY") ?: ""
def baseUrl = System.getenv("BASE_URL") ?: ""
//def googleMapsApiKey = System.getenv("GOOGLE_MAPS_API_KEY") ?: ""
//def baseUrl = System.getenv("BASE_URL") ?: ""

android {
namespace 'org.technoserve.farmcollector'
Expand All @@ -25,8 +25,8 @@ android {
vectorDrawables {
useSupportLibrary true
}
buildConfigField "String", "GOOGLE_MAPS_API_KEY", "\"${googleMapsApiKey}\""
buildConfigField "String", "BASE_URL", "\"${baseUrl}\""
// buildConfigField "String", "GOOGLE_MAPS_API_KEY", "\"${googleMapsApiKey}\""
// buildConfigField "String", "BASE_URL", "\"${baseUrl}\""
}

buildTypes {
Expand Down Expand Up @@ -218,6 +218,21 @@ dependencies {

// androidTestImplementation "androidx.navigation:navigation-testing:2.8.4"

testImplementation "org.robolectric:robolectric:4.5.1"
testImplementation "org.assertj:assertj-core:3.14.0"
androidTestImplementation"androidx.test.espresso:espresso-intents:3.6.1"

androidTestImplementation("androidx.test:core:kotlinx-coroutines-android-junit4:1.9.0")
androidTestImplementation("org.robolectric:robolectric:4.7.6")
androidTestImplementation "androidx.work:work-testing:2.8.0"
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.9.0")
testImplementation("org.mockito:mockito-core:3.12.4")
testImplementation "androidx.arch.core:core-testing:2.1.0"





androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.6.8"
debugImplementation "androidx.compose.ui:ui-test-manifest:1.6.8"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class FarmCollectorApp : Application() {
initializeWorkManager()
}

private fun initializeWorkManager() {
// private
fun initializeWorkManager() {
val workRequest = PeriodicWorkRequestBuilder<SyncWorker>(2, TimeUnit.HOURS)
.setConstraints(
Constraints.Builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
package org.technoserve.farmcollector

//import org.junit.Before
//import org.junit.Test
//import org.junit.runner.RunWith
//import org.mockito.Mock
//import org.mockito.Mockito.`when`
//import org.mockito.junit.MockitoJUnitRunner
//import org.technoserve.farmcollector.database.CalculatorExample
//import org.technoserve.farmcollector.database.Operators
//
//@RunWith(MockitoJUnitRunner::class)
//class CalculatorExampleTest {
//
// lateinit var CE: CalculatorExample
//
// @Mock
// lateinit var OP: Operators
//
// @Before
// fun onSetup() {
// CE = CalculatorExample(OP)
// }
//
// @Test
// fun addTwoNumber_PrintValue() {
// val a = 100
// val b = 20
//
// `when`(OP.addTwoInt(a, b)).thenReturn(a + b)
//
// val result = CE.addTwoNumbers(a, b)
//
// println(" after add two number : $result")
// }
//}
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.junit.MockitoJUnitRunner
import org.technoserve.farmcollector.database.CalculatorExample
import org.technoserve.farmcollector.database.Operators

@RunWith(MockitoJUnitRunner::class)
class CalculatorExampleTest {

lateinit var CE: CalculatorExample

@Mock
lateinit var OP: Operators

@Before
fun onSetup() {
CE = CalculatorExample(OP)
}

@Test
fun addTwoNumber_PrintValue() {
val a = 100
val b = 20

`when`(OP.addTwoInt(a, b)).thenReturn(a + b)

val result = CE.addTwoNumbers(a, b)

println(" after add two number : $result")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.technoserve.farmcollector

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.work.Constraints
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.NetworkType
import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.anyString
import org.mockito.Mockito
import org.mockito.Mockito.mockStatic
import java.util.concurrent.TimeUnit

class FarmCollectorAppTest {

@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()

private lateinit var mockWorkManager: WorkManager
private lateinit var app: FarmCollectorApp

@Before
fun setup() {
mockWorkManager = Mockito.mock(WorkManager::class.java)
mockStatic(WorkManager::class.java).use { mockedStatic ->
mockedStatic.`when`<Any> { WorkManager.getInstance(any()) }.thenReturn(mockWorkManager)
}
app = FarmCollectorApp()
}

@Test
fun `test onCreate initializes WorkManager`() {
// Arrange
val expectedTag = "sync_work_tag"
val expectedPolicy = ExistingPeriodicWorkPolicy.UPDATE

// Act
app.onCreate()

// Assert
Mockito.verify(mockWorkManager).enqueueUniquePeriodicWork(
Mockito.eq(expectedTag),
Mockito.eq(expectedPolicy),
Mockito.any()
)
}

@Test
fun `test initializeWorkWorker creates PeriodicWorkRequest`() {
// Arrange
val expectedInterval = 2L
val expectedTimeUnit = TimeUnit.HOURS

// Act
val workRequest = app.initializeWorkManager()

// Assert
Mockito.verify(mockWorkManager).enqueueUniquePeriodicWork(
anyString(),
Mockito.any(),
Mockito.argThat { request: PeriodicWorkRequest ->
request.workSpec.intervalDuration == TimeUnit.HOURS.toMillis(expectedInterval) &&
request.workSpec.constraints.requiredNetworkType == NetworkType.CONNECTED
}
)
}

@Test
fun `test initializeWorkWorker throws exception when network not connected`() {
// Arrange
val mockConstraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.NOT_REQUIRED)
.build()
Mockito.`when`(app.initializeWorkManager()).thenThrow(Exception("Required network type is not connected"))

// Act & Assert
try {
app.initializeWorkManager()
fail("Expected an exception to be thrown")
} catch (exception: Exception) {
assertTrue(exception.message?.contains("Required network type") == true)
}
}

}
Loading

0 comments on commit 32bb5d7

Please sign in to comment.