Unit testing 当单元测试我的ViewModel时,列表总是返回空
我试图学习单元测试,我将代码分解到MVVM(I)体系结构中,但是当我运行testParseToList()测试函数时,它总是返回一个空列表,我不知道为什么。我担心这可能和MVVM(I)的I部分有关,也可能和我是否正确地模拟了viewmodel有关。我从我最简单的viewmodel开始,希望在进入更复杂的概念之前先掌握这些概念 OfflineViewModelUnitTest.ktUnit testing 当单元测试我的ViewModel时,列表总是返回空,unit-testing,kotlin,junit,mockito,Unit Testing,Kotlin,Junit,Mockito,我试图学习单元测试,我将代码分解到MVVM(I)体系结构中,但是当我运行testParseToList()测试函数时,它总是返回一个空列表,我不知道为什么。我担心这可能和MVVM(I)的I部分有关,也可能和我是否正确地模拟了viewmodel有关。我从我最简单的viewmodel开始,希望在进入更复杂的概念之前先掌握这些概念 OfflineViewModelUnitTest.kt @RunWith(JUnit4::class) class OfflineViewModelUnitTest {
@RunWith(JUnit4::class)
class OfflineViewModelUnitTest {
@get:Rule
val rule = InstantTaskExecutorRule()
@Mock
var offlineViewModel: OfflineViewModel = OfflineViewModel(OfflineInteractorImpl())
@Before
fun setup() {
MockitoAnnotations.initMocks(this)
DaggerOfflineViewModelComponent.builder()
.offlineInteractorImplModule(OfflineInteractorImplModule())
.build()
.inject(offlineViewModel)
// this.offlineViewModel = OfflineViewModel(OfflineInteractorImpl())
}
@Test
fun testParseToList() {
val test = offlineViewModel.parseTextToList("dried bonito extract,\n" +
" ketchup,\n" +
" millet,\n" +
" corn & wheat protein")
val a = "dried bonito extract"
val b = "ketchup"
val c = "millet"
val d = "corn & wheat protein"
val expectedResult = listOf(a, b, c, d)
assertEquals(expectedResult, test)
}
}
class OfflineViewModel(private val offlineInteractor: OfflineInteractor): ViewModel() {
init {
DaggerOfflineViewModelComponent.builder()
.offlineInteractorImplModule(OfflineInteractorImplModule())
.build()
.inject(this)
}
fun parseTextToList(firebaseVisionTextString: String): MutableList<String> {
Log.d("here it is", firebaseVisionTextString)
return offlineInteractor.parseTextToList(firebaseVisionTextString)
}
fun readCsvFromAssetFolder(inputStream: InputStream): List<String>{
return offlineInteractor.readCsvFromAssetFolder(inputStream)
}
}
class OfflineInteractorImpl: OfflineInteractor {
override fun parseTextToList(firebaseVisionTextString: String): MutableList<String> {
val ingredientsList: MutableList<String> = firebaseVisionTextString.split(",").map { it.trim() }.toMutableList()
return ingredientsList
}
override fun readCsvFromAssetFolder(inputStream: InputStream): List<String> {
val csvLine: ArrayList<String> = ArrayList()
var content: Array<String>?
try
{
val br = BufferedReader(InputStreamReader(inputStream))
for (line in br.lines())
{
content = line.split((",").toRegex()).dropLastWhile{ it.isEmpty() }.toTypedArray()
csvLine.add(content[0].substringBefore(";"))
}
br.close()
}
catch (e: IOException) {
e.printStackTrace()
}
return csvLine
}
}
离线视图模型.kt
@RunWith(JUnit4::class)
class OfflineViewModelUnitTest {
@get:Rule
val rule = InstantTaskExecutorRule()
@Mock
var offlineViewModel: OfflineViewModel = OfflineViewModel(OfflineInteractorImpl())
@Before
fun setup() {
MockitoAnnotations.initMocks(this)
DaggerOfflineViewModelComponent.builder()
.offlineInteractorImplModule(OfflineInteractorImplModule())
.build()
.inject(offlineViewModel)
// this.offlineViewModel = OfflineViewModel(OfflineInteractorImpl())
}
@Test
fun testParseToList() {
val test = offlineViewModel.parseTextToList("dried bonito extract,\n" +
" ketchup,\n" +
" millet,\n" +
" corn & wheat protein")
val a = "dried bonito extract"
val b = "ketchup"
val c = "millet"
val d = "corn & wheat protein"
val expectedResult = listOf(a, b, c, d)
assertEquals(expectedResult, test)
}
}
class OfflineViewModel(private val offlineInteractor: OfflineInteractor): ViewModel() {
init {
DaggerOfflineViewModelComponent.builder()
.offlineInteractorImplModule(OfflineInteractorImplModule())
.build()
.inject(this)
}
fun parseTextToList(firebaseVisionTextString: String): MutableList<String> {
Log.d("here it is", firebaseVisionTextString)
return offlineInteractor.parseTextToList(firebaseVisionTextString)
}
fun readCsvFromAssetFolder(inputStream: InputStream): List<String>{
return offlineInteractor.readCsvFromAssetFolder(inputStream)
}
}
class OfflineInteractorImpl: OfflineInteractor {
override fun parseTextToList(firebaseVisionTextString: String): MutableList<String> {
val ingredientsList: MutableList<String> = firebaseVisionTextString.split(",").map { it.trim() }.toMutableList()
return ingredientsList
}
override fun readCsvFromAssetFolder(inputStream: InputStream): List<String> {
val csvLine: ArrayList<String> = ArrayList()
var content: Array<String>?
try
{
val br = BufferedReader(InputStreamReader(inputStream))
for (line in br.lines())
{
content = line.split((",").toRegex()).dropLastWhile{ it.isEmpty() }.toTypedArray()
csvLine.add(content[0].substringBefore(";"))
}
br.close()
}
catch (e: IOException) {
e.printStackTrace()
}
return csvLine
}
}
就像第二个所说的,由于您模拟了offlineViewModel,它将返回一个空字符串,除非您使用when()为它定义要返回的内容
来源:
offlineViewModel
是您正在测试的类,但您模拟了它。你不应该模仿被测试的类。此外,如果创建模拟,则需要为其定义行为,否则它将只返回null
(或与之等效的原语)。