kotlin 1.2.51
I have the following shared preferences that uses a generic extension function.
class SharedUserPreferencesImp(private val context: Context,
private val sharedPreferenceName: String): SharedUserPreferences {
private val sharedPreferences: SharedPreferences by lazy {
context.getSharedPreferences(sharedPreferenceName, Context.MODE_PRIVATE)
}
override fun <T : Any> T.getValue(key: String): T {
with(sharedPreferences) {
val result: Any = when (this@getValue) {
is String -> getString(key, this@getValue)
is Boolean -> getBoolean(key, this@getValue)
is Int -> getInt(key, this@getValue)
is Long -> getLong(key, this@getValue)
is Float -> getFloat(key, this@getValue)
else -> {
throw UnsupportedOperationException("Cannot find preference casting error")
}
}
@Suppress("unchecked_cast")
return result as T
}
}
}
I am trying to write a unit test for this method. As you can see in my test method the testName.getValue("key")
the getValue
is not recognized.
class SharedUserPreferencesImpTest {
private lateinit var sharedUserPreferences: SharedUserPreferences
private val context: Context = mock()
@Before
fun setUp() {
sharedUserPreferences = SharedUserPreferencesImp(context, "sharedPreferenceName")
assertThat(sharedUserPreferences).isNotNull
}
@Test
fun `should get a string value from shared preferences`() {
val testName = "this is a test"
testName.getValue("key")
}
}
What is the best way to test a extension function that has a generic type?
Many thanks for any suggestions,