如何为Android Kotlin中的switch条件编写测试用例。

3

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

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)
    }

上述测试用例的功能良好。我需要找出如何为该类的 switch 条件编写测试用例。请帮助我编写相同的内容。

1个回答

1
我还没有回答你的问题,但也许稍微重构一下你的代码会让测试更加明显:
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”。 - Christina Varghese
java.lang.IllegalStateException: properties.getProperty("prodUrl") must not be null 这是我得到的异常。你知道如何测试包含属性文件的方法吗?似乎属性文件没有被调用。 - Christina Varghese
我认为是因为你正在使用PowerMockito来创建“Properties”变量。你需要模拟“getProperty”方法,像这样:Mockito.when(testProperties.getProperty(anyString()).thenReturn("expectedProperty") - Blundell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接