简体   繁体   中英

How to unit test an kotlin extenion function that has a generic type

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,

There is a conflict between T.getValue(key: String) being a extension function and SharedUserPreferencesImp member function. You can make T.getValue(key: String) high-level function and this solves a problem. Here is example code:

fun <T : Any> T.getValue(key: String, sharedPreferences: SharedUserPreferencesImp): T {
    with(sharedPreferences.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
    }
}

class SharedUserPreferencesImp(private val context: Context,
                               private val sharedPreferenceName: String): SharedUserPreferences {

    val sharedPreferences: SharedPreferences by lazy {
        context.getSharedPreferences(sharedPreferenceName, Context.MODE_PRIVATE)
    }
}

You can also take a look at this two great libraries: https://github.com/chibatching/Kotpref https://github.com/MarcinMoskala/PreferenceHolder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM