Skip to content

Instantly share code, notes, and snippets.

@nglauber
Created September 8, 2018 13:07
Show Gist options
  • Save nglauber/2dd0e6c1377013d1495dd353f8eef884 to your computer and use it in GitHub Desktop.
Save nglauber/2dd0e6c1377013d1495dd353f8eef884 to your computer and use it in GitHub Desktop.
dependencies {
testImplementation 'junit:junit:4.12'
testImplementation "androidx.arch.core:core-testing:2.0.0-rc01"
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0-alpha03@jar"
}
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.lifecycle.Observer
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify
import org.junit.Rule
import org.junit.Test
class ExampleUnitTest {
@Rule
@JvmField
val rule = InstantTaskExecutorRule()
@Test
fun testLiveData() {
// Given
val meuViewModel = MeuViewModel()
val observer = mock<Observer<Int>>()
meuViewModel.getValor().observeForever(observer)
// When
meuViewModel.atualizarValor(10)
// Then
verify(observer).onChanged(10)
}
}
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
class MeuViewModel : ViewModel() {
private val valor = MutableLiveData<Int>()
fun getValor(): LiveData<Int> = valor
fun atualizarValor(v: Int) {
valor.value = v
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment