0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Android] オブジェクトをMockする

Posted at

単体Test時に、ObjectをMockしたい時があったのでメモ。

class MainViewModel : ViewModel() {
    
    private val _date = MutableLiveData<String>()
    val date: LiveData<String>
        get() = _date

    fun fetchDate() {
        val sdf = SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.JAPAN)
        _date.value = sdf.format(DateObj.getCurrent())
    }

}

上記のViewModelをTestするときに、DataObjをMockしてみる。

@Before
fun beforeTests() {
    mockkObject(DateObj)
    every { DateObj.getCurrent() } returns Data()
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?