繁体   English   中英

如何在android kotlin中编写switch条件的测试用例

[英]How to write test cases for switch condition in android kotlin

我需要在kotlin为switch状态编写测试用例。

Class.kt

fun getEnvSwitchURL(applicationContext: Context, envSwitchInfo: String): String {
        val resources = applicationContext.getResources()
        val assetManager = resources.getAssets()
        val properties = Properties()
        try {
            val inputStream = assetManager.open("configuration.properties")
            properties.load(inputStream)
            val urlPref = applicationContext.getSharedPreferences(SELECTED_ENV, Context.MODE_PRIVATE)
            val editor = urlPref.edit()
            when (envSwitchInfo) {
                "Production" ->{
                    editor.putString("selectedUrl", properties.getProperty("prodUrl"))
                    editor.apply()
                    selectedUrl=properties.getProperty("prodUrl")
                }
                "Development" ->{
                    editor.putString("selectedUrl", properties.getProperty("devUrl"))
                    editor.apply()
                    selectedUrl=properties.getProperty("devUrl")
                }
                "Testing" ->{
                    editor.putString("selectedUrl", properties.getProperty("testUrl"))
                    editor.apply()
                    selectedUrl=properties.getProperty("testUrl")
                }
            }
            inputStream.close()
        }
        return selectedUrl
    }

test.kt

@BeforeEach
    fun runBeforeTest() {

        testApplicationContext = Mockito.mock(Context::class.java)
        testResource = Mockito.mock(Resources::class.java)
        testAsset = Mockito.mock(AssetManager::class.java)
        testInputStream = Mockito.mock(InputStream::class.java)
        testSharedPref=Mockito.mock(SharedPreferences::class.java)
        testEditor=Mockito.mock(SharedPreferences.Editor::class.java)
        testProperties=Mockito.mock(Properties::class.java)
        testProperties.setProperty("prodUrl", "Value");
    }

@Test
    fun getEnvSwitchURL() {
        Mockito.`when`(testApplicationContext.getResources()).thenReturn(testResource)
        Mockito.`when`(testResource.assets).thenReturn(testAsset)
        Mockito.`when`(testAsset.open(Mockito.anyString())).thenReturn(testInputStream)
        PowerMockito.whenNew(Properties::class.java).withNoArguments().thenReturn(testProperties)
        Mockito.doNothing().`when`(testProperties).load(Mockito.any(InputStream::class.java))
        Mockito.`when`(testApplicationContext.getSharedPreferences(anyString(),anyInt())).thenReturn(testSharedPref)
        Mockito.`when`(testSharedPref.edit()).thenReturn(testEditor)
        envSwitchUtils.getEnvSwitchURL(testApplicationContext, testEnvSwitchInfo)
    }

上面写的测试用例工作正常。 我需要找出如何为上面的类编写开关条件的测试用例。 请帮我写同样的东西

我没有回答你的问题,但也许稍微重构你的代码会让测试变得更加明显:

private val SELECTED_ENV = "";

fun getEnvSwitchURL(applicationContext: Context, envSwitchInfo: String): String {
    val resources = applicationContext.resources
    val assetManager = resources.assets
    val properties = Properties()
    val selectedUrl: String
    try {
        val inputStream = assetManager.open("configuration.properties")
        properties.load(inputStream)
        val urlPref = applicationContext.getSharedPreferences(SELECTED_ENV, Context.MODE_PRIVATE)
        val editor = urlPref.edit()
        selectedUrl = get(envSwitchInfo, properties)
        editor.putString("selectedUrl", selectedUrl)
        editor.apply()
        inputStream.close()
    }
    return selectedUrl
}

fun get(envSwitchInfo: String, properties: Properties): String {
    when (envSwitchInfo) {
        "Production" -> {
            return properties.getProperty("prodUrl")
        }
        "Development" -> {
            return properties.getProperty("devUrl")
        }
        "Testing" -> {
            return properties.getProperty("testUrl")
        }
        else -> throw IllegalStateException("Unhandled environment $envSwitchInfo")
    }
}

你可以在这里做更多的事情,看看单一责任原则。 这是一个开始,对于单元测试,您不希望测试SharePreferences是否正常工作,因为那时您正在测试平台而不是您的代码。 您可能只想测试当您通过“生产”之类的环境时,会返回您获得的selectedUrl。

如上所述测试输入和输出将是这样的:

 String url = envSwitchUtils.getEnvSwitchURL(testApplicationContext, "Production")
 assertEquals(url, "http://myProdUrl")

和另一个测试

 String url = envSwitchUtils.getEnvSwitchURL(testApplicationContext, "Development")
 assertEquals(url, "http://myDevUrl")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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