Created
September 8, 2018 13:07
-
-
Save nglauber/2dd0e6c1377013d1495dd353f8eef884 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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