我已经查看了源代码,我唯一想知道为什么这两个版本都存在一个应该被弃用,不是吗?
今天我正在为我使用的存储库类中的一个 viewModel 类编写单元测试android.util.ArrayMap。我确实用假值初始化了 ArrayMap,但在调试时发现 arrayMap 大小始终为零。
arrayMap["key1"] = someValue
arrayMap["key2"] = someValue
arrayMap["key3"] = someValue
但是当我改变它时androidx.collection.ArrayMap它工作正常。但问题是我已经android.util.ArrayMap在我的存储库和 viewModel 类中使用了,此时我不想将其转换为androidx.collection.ArrayMap.
这里是测试类。
import android.util.ArrayMap
//import androidx.collection.ArrayMap // switching to this works fine
import org.junit.*
import org.junit.Assert.assertNotNull
import org.junit.runners.MethodSorters
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class AudioViewModelTest {
private lateinit var audioMap: ArrayMap<Int, AudioItem>
@Before
fun setUp() {
audioMap = ArrayMap()
val audioItem = AudioItem()
audioMap[1] = audioItem
audioMap[2] = audioItem
audioMap[3] = audioItem
assertNotNull(audioMap[1])// this is failing
}
@Test
fun testAudioMap() {
assertNotNull(audioMap[1])// this is failing
}
}
有人可以解释为什么android.util.ArrayMap即使我在其中添加了一些元素,为什么总是将大小设为零。