Android单元测试:如何模拟包含MutableLiveData但仅公开LiveData的对象?

问题描述 投票:1回答:1

我有一个存储库类,该类使用MutableLiveData对象(仅作为LiveData公开)将异步Web查询的结果返回给ViewModel。然后,ViewModel使用Transformation将结果映射到另一个View观察到的MutableLiveData。

我认为我通过分离关注点遵循了本模块中推荐的体系结构,但是我发现很难为ViewModel编写单元测试:

class DataRepository ( private val webservice: DataWebService ) {

    private val _exception = MutableLiveData<Exception?>(null)
    val exception : LiveData<Exception?> get() = _exception

    private val _data = MutableLiveData<List<DataItem>>()

    val data: LiveData<List<DataItem>> = _data

    private val responseListener = Response.Listener<String> {response ->
        try {
            val list = JsonReader(SearchResult.mapping).readObject(response).map {
                    //Data transformation
            }
            _exception.value = null
            _data.value = list
        } catch (ex: Exception) {
            _exception.value = ex
            _data.value = emptyList()
        } 
    }

    fun findData(searchString: String) {
        _data.value = emptyList()
        webservice.findData(searchString, responseListener = responseListener)
    } 
}
class WebServiceDataViewModel (private val repository: DataRepository, app: App) : AndroidViewModel(app)
{

    val dataList: LiveData<List<DataItem>> = Transformations.map(repository.data) {
        _showEmpty.value = it.isEmpty()
        it
    }
    val exception: LiveData<Exception?> get() = repository.exception

    private val _showEmpty = MutableLiveData(true)
    val showEmpty : LiveData<Boolean> = _showEmpty

    private var _reloadOnCreate = true

    var searchString: String? = null
        set(value) {
            field = value
            if (!value.isNullOrBlank()) {
                repository.findData(value)
            }
        }
}

ViewModel测试类:

@RunWith(JUnit4::class)
class WebServicePodcastViewModelTest {
    @Rule var instantExecutorRule = InstantTaskExecutorRule()

    @Mock lateinit var repository : DataRepository
    @Mock lateinit var app : App

    lateinit var viewModel: WebServiceDataViewModel

    @Mock lateinit var exceptionObserver : Observer<Exception?>
    @Mock lateinit var dataObserver : Observer<List<DataItem>>
    @Mock lateinit var showEmptyObserver : Observer<Boolean>

    @Before
    fun setUp() {
        MockitoAnnotations.initMocks(this)

        viewModel = WebServiceDataViewModel(repository, app)
        viewModel.exception.observeForever(exceptionObserver)
        viewModel.showEmpty.observeForever(showEmptyObserver)
        viewModel.dataList.observeForever(dataObserver)
    }

    @Test
    fun searchForData() {
        //given
        val searchString = "MockSearch"
        //when
        `when`(repository.findData(searchString)).then { /* How to invoke the mock repositories LiveData? */ }
        //then
        //TODO: verify that ViewModel LiveData behaves as expected
    }
}

因此,如何调用模拟类的不可变LiveData?目前,我正在尝试使用Mockito和JUnit,但是如果设置简单,我会接受不同的框架!

android junit mockito android-livedata mutablelivedata
1个回答
0
投票

最后,我抛弃了Mockito,改用MockK,它就像一个吊饰!

© www.soinside.com 2019 - 2024. All rights reserved.