带有实时数据的单元测试始终在android.os.oper中获取错误&quot getmainlooper。

发布于 2025-02-04 03:35:04 字数 1569 浏览 3 评论 0原文

在我的ViewModel中,我使用实时数据持有响应状态。在这里,代码:

@HiltViewModel
class MainViewModel @Inject constructor(private val repository: Repository): ViewModel() {

    private val _charData = MutableLiveData<Response<List<Character>>>()

    val charData: LiveData<Response<List<Character>>>
        get() = _charData

    init {
        getCharacters()
    }

    fun getCharacters(){
        viewModelScope.launch {
            _charData.value = repository.getCharacters()
        }
    }
}

我的测试总是会因该错误而失败,并且问题在行_CHARDATA.VALUE = repository.getCharacters()中。我还查看了相同的问题,应该通过添加@get:unial val instantexecutorrule:instanttaskexecutorrule = instanttaskexecutorrule()来处理。但是,错误仍然存​​在。在这里我的测试代码:

@RunWith(JUnit4::class)
class MainViewModelTest {
    @get:Rule
    val mainRule =  MainCoroutineRule()

    lateinit var repository: Repository

    lateinit var viewModel: MainViewModel

    @get:Rule
    val instantExecutorRule: InstantTaskExecutorRule = InstantTaskExecutorRule()

    @Before
    fun setUp(){
        MockitoAnnotations.openMocks(this)
        repository = MockRepository()
        viewModel = MainViewModel(repository)
    }

    @Test
    fun getCharactersSuccess() = runTest {
        val observer = mock<Observer<Response<List<Character>>>>()
        viewModel.getCharacters()
        viewModel.charData.observeForever(observer)
        assertTrue(viewModel.charData.value is Response.Success)
    }
}

我应该如何解决此错误? 谢谢

In my ViewModel, I use live data to hold the response state. Here the code:

@HiltViewModel
class MainViewModel @Inject constructor(private val repository: Repository): ViewModel() {

    private val _charData = MutableLiveData<Response<List<Character>>>()

    val charData: LiveData<Response<List<Character>>>
        get() = _charData

    init {
        getCharacters()
    }

    fun getCharacters(){
        viewModelScope.launch {
            _charData.value = repository.getCharacters()
        }
    }
}

My test always fails with that error and the problem is in the line _charData.value = repository.getCharacters(). I also have looked at the same problem and it should be handled by adding @get:Rule val instantExecutorRule: InstantTaskExecutorRule = InstantTaskExecutorRule(). However, the error still persists. Here my test code:

@RunWith(JUnit4::class)
class MainViewModelTest {
    @get:Rule
    val mainRule =  MainCoroutineRule()

    lateinit var repository: Repository

    lateinit var viewModel: MainViewModel

    @get:Rule
    val instantExecutorRule: InstantTaskExecutorRule = InstantTaskExecutorRule()

    @Before
    fun setUp(){
        MockitoAnnotations.openMocks(this)
        repository = MockRepository()
        viewModel = MainViewModel(repository)
    }

    @Test
    fun getCharactersSuccess() = runTest {
        val observer = mock<Observer<Response<List<Character>>>>()
        viewModel.getCharacters()
        viewModel.charData.observeForever(observer)
        assertTrue(viewModel.charData.value is Response.Success)
    }
}

How should I fix this error?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恋你朝朝暮暮 2025-02-11 03:35:04

添加

testOptions {
            unitTests.returnDefaultValues = true
        }

add

testOptions {
            unitTests.returnDefaultValues = true
        }
半世蒼涼 2025-02-11 03:35:04

我找到了解决方案。
您可以使用robolectric。
@runwith(MockitoJunitRunner :: class)在您的ViewModel test类中替换

testImplementation "org.robolectric:robolectric:4.11.1"

I find the solution.
You can use Robolectric.
Replace @RunWith(MockitoJUnitRunner::class) with in your ViewModel Test class @RunWith(RobolectricTestRunner::class)

Also use the below dependency for rorolectric

testImplementation "org.robolectric:robolectric:4.11.1"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文