Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android kotlin中编写切换条件测试用例_Android_Unit Testing_Kotlin_Junit - Fatal编程技术网

如何在android kotlin中编写切换条件测试用例

如何在android kotlin中编写切换条件测试用例,android,unit-testing,kotlin,junit,Android,Unit Testing,Kotlin,Junit,我需要为kotlin中的开关条件编写测试用例 Class.kt test.kt 上述书面测试用例运行良好。我需要了解如何为上述类编写切换条件测试用例。请帮我写同样的我还没有回答你的问题,但也许稍微重构一下你的代码会让测试变得更加明显: private val SELECTED_ENV = ""; fun getEnvSwitchURL(applicationContext: Context, envSwitchInfo: String): String { val resources

我需要为
kotlin
中的开关条件编写测试用例

Class.kt

test.kt


上述书面测试用例运行良好。我需要了解如何为上述类编写切换条件测试用例。请帮我写同样的

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

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是否正常工作,因为这样您就可以测试平台,而不是代码。您可能只想在通过“Production”之类的环境时测试,然后返回您得到的selectedUrl

如上所述,测试输入和输出如下:

 String url = envSwitchUtils.getEnvSwitchURL(testApplicationContext, "Production")
 assertEquals(url, "http://myProdUrl")
还有一个测试

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

谢谢你的帮助,代码看起来很简单。但是您编写的测试不起作用,它显示为
nullpointerexception
java.lang.IllegalStateException:properties.getProperty(“prodUrl”)不能为null
。这是我得到的例外。因此,您知道如何测试包含属性文件的方法吗?似乎属性文件没有被调用。我认为是因为您使用PowerMockito创建了
Properties
变量。您需要模拟
getProperty
方法,类似于:
Mockito.
when
(testProperties.getProperty(anyString())。然后返回(“expectedProperty”)
 String url = envSwitchUtils.getEnvSwitchURL(testApplicationContext, "Production")
 assertEquals(url, "http://myProdUrl")
 String url = envSwitchUtils.getEnvSwitchURL(testApplicationContext, "Development")
 assertEquals(url, "http://myDevUrl")