我有以下简单ArrayAdapter
的东西,我想在上面运行 UnitTests:
class AccountSpinnerAdapter(context: Context, textViewResourceId: Int, private val values: Set<Account>) : ArrayAdapter<Account>(context, textViewResourceId, values.toList()) {
override fun getCount() = values.size
override fun getItem(position: Int) = values.elementAt(position)
override fun getItemId(position: Int) = position.toLong()
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val label = super.getView(position, convertView, parent) as TextView
label.text = values.elementAt(position).displayName
return label
}
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
val label = super.getDropDownView(position, convertView, parent) as TextView
label.text = values.elementAt(position).displayName
return label
}
}
getCount
,getItem
并且getItemId
很容易做到。
但是我怎样才能测试getView
和getDropDownView
方法呢?我面临的问题是:
- 第三个参数
viewGroup
不能null
。如何伪造/模拟这个ViewGroup
? - 两种方法都在调用
super.xxx
。我怎样才能建立一个when/then
构造让它返回一个TextView
?