如何在安卓 Kotlin 中为开关条件编写测试用例



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

类.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")

最新更新