-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4842df
commit 32bb5d7
Showing
10 changed files
with
995 additions
and
45 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 34 additions & 34 deletions
68
app/src/test/java/org/technoserve/farmcollector/CalculatorExampleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
app/src/test/java/org/technoserve/farmcollector/FarmCollectorAppTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.