如何在 Android Kotlin 中测试 ViewModel Factory 类

发布于 2025-01-09 13:13:01 字数 1371 浏览 0 评论 0原文

我有下面的类,对于 ViewModel Fatory

class MovieViewModelFactory(private val movieUseCase: MovieUseCase) :
    ViewModelProvider.Factory {
  override fun <T : ViewModel> create(modelClass: Class<T>): T {
    return if (modelClass.isAssignableFrom(
            MovieViewModel::class.java
        )
    ) MovieViewModel(movieUseCase) as T
    else throw RuntimeException("Illegal View Model class Found")
  }
}

应用 Mockito 测试工厂方法,我能够测试 ViewModel,但想测试 ViewModelFactory。

class MovieViewModelTest {
  @Rule @JvmField val instantExecutorRule = InstantTaskExecutorRule()

  @get:Rule var initRule: MockitoRule = MockitoJUnit.rule()

  @ExperimentalCoroutinesApi @get:Rule var mainCoroutineRule = MainCoroutineRule()

  private val repository: MovieRepository = Mockito.mock(MovieRepository::class.java)

  private val movieUseCase: MovieUseCase = MovieUseCase(repository)

  private lateinit var viewModel: MovieViewModel

  val viewModelFactory: MovieViewModelFactory = Mockito.mock(MovieViewModelFactory::class.java)

  @Before fun initViewModel() {
    MockitoAnnotations.initMocks(this)
    viewModel = MovieViewModel(movieUseCase)
  }
  @Test fun setupViewModelFactory() {
    Mockito.`when`(viewModelFactory.create(MovieViewModel::class.java)).thenReturn(viewModel)
    Assert.assertNotNull(viewModel)
  }
}

I have the below class For the ViewModel Fatory

class MovieViewModelFactory(private val movieUseCase: MovieUseCase) :
    ViewModelProvider.Factory {
  override fun <T : ViewModel> create(modelClass: Class<T>): T {
    return if (modelClass.isAssignableFrom(
            MovieViewModel::class.java
        )
    ) MovieViewModel(movieUseCase) as T
    else throw RuntimeException("Illegal View Model class Found")
  }
}

Applying Mockito To Test The Factory Method , I am able to test the ViewModel but want to test the ViewModelFactory.

class MovieViewModelTest {
  @Rule @JvmField val instantExecutorRule = InstantTaskExecutorRule()

  @get:Rule var initRule: MockitoRule = MockitoJUnit.rule()

  @ExperimentalCoroutinesApi @get:Rule var mainCoroutineRule = MainCoroutineRule()

  private val repository: MovieRepository = Mockito.mock(MovieRepository::class.java)

  private val movieUseCase: MovieUseCase = MovieUseCase(repository)

  private lateinit var viewModel: MovieViewModel

  val viewModelFactory: MovieViewModelFactory = Mockito.mock(MovieViewModelFactory::class.java)

  @Before fun initViewModel() {
    MockitoAnnotations.initMocks(this)
    viewModel = MovieViewModel(movieUseCase)
  }
  @Test fun setupViewModelFactory() {
    Mockito.`when`(viewModelFactory.create(MovieViewModel::class.java)).thenReturn(viewModel)
    Assert.assertNotNull(viewModel)
  }
}

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

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

发布评论

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

评论(1

荆棘i 2025-01-16 13:13:01

这适用于基本的成功/失败测试。

class MyViewModelFactoryTest {

private lateinit var factory: MyViewModelFactory

@Test
    fun `create should return MyViewModel when requested`() {
        val viewModel = factory.create(MyViewModel::class.java)

        assertIs<MyViewModel>(viewModel)
    }

    @Test
    fun `create should throw IllegalArgumentException for unknown ViewModel class`() {
        assertFailsWith<IllegalArgumentException> {
            factory.create(UnknownViewModel::class.java)
        }
    }

class UnknownViewModel : ViewModel()

}

This worked for basic success/fail tests.

class MyViewModelFactoryTest {

private lateinit var factory: MyViewModelFactory

@Test
    fun `create should return MyViewModel when requested`() {
        val viewModel = factory.create(MyViewModel::class.java)

        assertIs<MyViewModel>(viewModel)
    }

    @Test
    fun `create should throw IllegalArgumentException for unknown ViewModel class`() {
        assertFailsWith<IllegalArgumentException> {
            factory.create(UnknownViewModel::class.java)
        }
    }

class UnknownViewModel : ViewModel()

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