【问题标题】:How to write test cases for switch condition in android kotlin如何在android kotlin中编写开关条件的测试用例
【发布时间】:2019-06-04 09:28:32
【问题描述】:

我需要为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)
    }

以上书面测试用例运行良好。我需要找出如何为上述类的开关条件编写测试用例。请帮我写同样的

【问题讨论】:

    标签: android unit-testing kotlin junit


    【解决方案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 是否正常工作,因为那时您正在测试平台而不是您的代码。您可能只想测试当您通过“生产”之类的环境时,您获得的 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") must not be null 。这是我得到的例外。那么你知道如何测试一个包含属性文件的方法吗,似乎属性文件没有被调用。
    • 我认为是因为您使用 PowerMockito 创建了 Properties 变量。您需要模拟 getProperty 方法,例如:Mockito.when(testProperties.getProperty(anyString()).thenReturn("expectedProperty")
    猜你喜欢
    • 2019-12-04
    • 2023-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多