Android-CleanArchitecture-Kotlin icon indicating copy to clipboard operation
Android-CleanArchitecture-Kotlin copied to clipboard

ViewModel tests are always successful

Open jmcaldera opened this issue 7 years ago • 5 comments

Assertions made inside the observeForever block are always successful, even when they shouldn't.

For example in MoviesViewModelTest, if i say that the first element poster is equals to "IronMannnn", it will pass, even if the real value is "IronMan".

jmcaldera avatar May 29 '18 22:05 jmcaldera

If you set a breakpoint inside the block of observeForever and debug, you'll see that it won't be reached. The reason is that GetMovies is mocked but the test gives behaviour only to run, so its method execute does nothing when is called by MoviesViewModel.

A solution might be spy GetMovies, so real methods are called (except run, which will be stubbed):

import org.mockito.Mockito.mock
import org.mockito.Mockito.spy

private val getMovies = spy(GetMovies(mock(MoviesRepository::class.java)))

JavierSA avatar Jun 10 '18 19:06 JavierSA

Good catch!

@JavierSA have you tried that out? If so, I would appreciate a PR.

Thanks!

android10 avatar Jun 25 '18 08:06 android10

@JavierSA - Unfortunately, in my project, UseCases cannot be run (due to networking dependencies) so, I had to mock the UseCases. My PR presents an alternate solution.

fowlerwill avatar Sep 04 '18 17:09 fowlerwill

@fowlerwill I think yours is better, so I've closed my PR.

JavierSA avatar Sep 26 '18 14:09 JavierSA

Thanks everyone for the contribution! I will review those PRs asap. :heart:

android10 avatar Sep 27 '18 07:09 android10