android10 / Android-CleanArchitecture-Kotlin

This is a movies sample app in Kotlin, which is part of a serie of blog posts I have written about architecting android application using different approaches.
https://fernandocejas.com/2018/05/07/architecting-android-reloaded/
4.69k stars 935 forks source link

ViewModel tests are always successful #27

Open jmcaldera opened 6 years ago

jmcaldera commented 6 years ago

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".

JavierSA commented 6 years ago

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)))
android10 commented 6 years ago

Good catch!

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

Thanks!

fowlerwill commented 6 years ago

@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.

JavierSA commented 6 years ago

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

android10 commented 6 years ago

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