AndroidAPS/wear/src/test/java/info/nightscout/androidaps/TestBase.kt

80 lines
2.8 KiB
Kotlin
Raw Normal View History

2021-12-11 00:27:17 +01:00
package info.nightscout.androidaps
import android.content.Context
import android.content.SharedPreferences
import info.nightscout.androidaps.interaction.utils.Persistence
import info.nightscout.androidaps.interaction.utils.WearUtil
import info.nightscout.androidaps.testing.mockers.WearUtilMocker
import info.nightscout.androidaps.testing.mocks.SharedPreferencesMock
2022-11-06 20:55:48 +01:00
import info.nightscout.rx.logging.AAPSLoggerTest
2022-05-03 20:33:34 +02:00
import info.nightscout.shared.sharedPreferences.SP
2022-11-29 15:03:25 +01:00
import info.nightscout.shared.utils.DateUtil
2021-12-11 00:27:17 +01:00
import org.junit.Before
2022-11-29 15:03:25 +01:00
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.extension.ExtendWith
2021-12-11 00:27:17 +01:00
import org.mockito.ArgumentMatchers
import org.mockito.Mock
import org.mockito.Mockito
2022-11-29 15:03:25 +01:00
import org.mockito.Mockito.`when`
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.junit.jupiter.MockitoSettings
import org.mockito.quality.Strictness
2022-11-06 20:55:48 +01:00
import java.util.Locale
2021-12-11 00:27:17 +01:00
2022-11-29 15:03:25 +01:00
@ExtendWith(MockitoExtension::class)
@MockitoSettings(strictness = Strictness.LENIENT)
2021-12-11 00:27:17 +01:00
open class TestBase {
@Mock lateinit var context: Context
2022-05-03 20:33:34 +02:00
@Mock lateinit var sp: SP
@Mock lateinit var dateUtil: DateUtil
2022-11-29 15:03:25 +01:00
@Mock lateinit var wearUtil: WearUtil
2021-12-11 00:27:17 +01:00
val aapsLogger = AAPSLoggerTest()
2022-11-29 15:03:25 +01:00
//val wearUtil: WearUtil = Mockito.mock(WearUtil::class.java)
lateinit var wearUtilMocker: WearUtilMocker
2021-12-11 00:27:17 +01:00
lateinit var persistence: Persistence
private val mockedSharedPrefs: HashMap<String, SharedPreferences> = HashMap()
2022-11-29 15:03:25 +01:00
@BeforeEach
2021-12-11 00:27:17 +01:00
fun setup() {
2022-11-29 15:03:25 +01:00
wearUtilMocker = WearUtilMocker(wearUtil)
2021-12-11 00:27:17 +01:00
Mockito.doAnswer { invocation ->
val key = invocation.getArgument<String>(0)
if (mockedSharedPrefs.containsKey(key)) {
return@doAnswer mockedSharedPrefs[key]
} else {
val newPrefs = SharedPreferencesMock()
mockedSharedPrefs[key] = newPrefs
return@doAnswer newPrefs
}
}.`when`(context).getSharedPreferences(ArgumentMatchers.anyString(), ArgumentMatchers.anyInt())
wearUtilMocker.prepareMockNoReal()
2022-11-29 15:03:25 +01:00
`when`(wearUtil.aapsLogger).thenReturn(aapsLogger)
`when`(wearUtil.context).thenReturn(context)
val rateLimits: MutableMap<String, Long> = HashMap()
`when`(wearUtil.rateLimits).thenReturn(rateLimits)
2022-05-03 20:33:34 +02:00
persistence = Mockito.spy(Persistence(aapsLogger, dateUtil, sp))
2021-12-11 00:27:17 +01:00
}
@Before
fun setupLocale() {
Locale.setDefault(Locale.ENGLISH)
System.setProperty("disableFirebase", "true")
}
// Workaround for Kotlin nullability.
// https://medium.com/@elye.project/befriending-kotlin-and-mockito-1c2e7b0ef791
// https://stackoverflow.com/questions/30305217/is-it-possible-to-use-mockito-in-kotlin
fun <T> anyObject(): T {
Mockito.any<T>()
return uninitialized()
}
@Suppress("Unchecked_Cast")
fun <T> uninitialized(): T = null as T
}