我有这段代码
carouselGroupMock = mock(ApiCarouselGroup::class.java).apply {
`when`(items).thenReturn(listOf("tvChannels", "featured"))
}
和蜂轮播组类 在这里
class ApiCarouselGroup @Throws(IOException::class)
constructor(jsonReader: JsonReader) : IApiPageLayoutComponent {
var items: List<String>? = null
init {
jsonReader.beginObject()
while (jsonReader.hasNext()) {
when (jsonReader.nextName()) {
"items" -> this.items = JsonReaderUtil.readArray(jsonReader, String::class.java)
else -> jsonReader.skipValue()
}
}
jsonReader.endObject()
}
override fun toPresenterModel(): PageComponent.CarouselGroup {
return PageComponent.CarouselGroup(items ?: emptyList())
}
}
interface IApiPageLayoutComponent {
fun toPresenterModel() : PageComponent
companion object {
val FACTORY : JsonReaderUtil.IObjectFactory<IApiPageLayoutComponent> = JsonReaderUtil.IObjectFactory<IApiPageLayoutComponent> {
jsonReader ->
val jsonObject = JsonReaderUtil.JSON_OBJECT_FACTORY.newInstance(jsonReader)
when(jsonObject.getString("type")) {
"heroBanners" -> ApiHeroBannerGroup(JsonReader(StringReader(jsonObject.toString())))
"carousels" -> ApiCarouselGroup(JsonReader(StringReader(jsonObject.toString())))
else -> null
}
};
}
}
问题是轮播组中的项目为空
只是为了提供信息,我想提一下我有这个,它没有问题 我不明白第一种情况有什么问题?
mainMenuMock = mock(ApiMainMenu::class.java).apply {)
`when`(actions).thenReturn(listOf(actionMock))
}
其中 ApiMainMenu
class ApiMainMenu @Throws(IOException::class)
constructor(jsonReader: JsonReader) {
var type: String? = null
var titles: List<ApiTitle>? = null
var actions: List<ApiAction>? = null
init {
jsonReader.beginObject()
while (jsonReader.hasNext()) {
when (jsonReader.nextName()) {
"action" -> this.actions = JsonReaderUtil.readArray(jsonReader, ApiAction::class.java)
else -> jsonReader.skipValue()
}
}
jsonReader.endObject()
}
}```
我不认为它真的回答了你的问题,但我认为你的模拟中有某个地方试图从最终课程中调用源代码。 您是否添加了此依赖项?
testImplementation 'org.mockito:mockito-inline:2.13.0' // for final class