Created
January 13, 2016 22:08
-
-
Save miensol/d2a3c070f9efc50d8d2f 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
package sample.stackanswers; | |
import android.app.Activity | |
import org.junit.Assert | |
import org.junit.Before; | |
import org.junit.Rule | |
import org.junit.Test | |
import org.junit.runner.RunWith; | |
import org.mockito.* | |
import org.mockito.junit.MockitoJUnit | |
import org.robolectric.RobolectricGradleTestRunner; | |
import org.robolectric.annotation.Config; | |
@RunWith(RobolectricGradleTestRunner::class) | |
@Config(sdk = intArrayOf(21), constants = BuildConfig::class) | |
class MyViewModelTest { | |
@Mock | |
lateinit var activityHandlerMock: IActivityHandler; | |
@Captor | |
lateinit var classCaptor: ArgumentCaptor<Class<Activity>> | |
@Captor | |
lateinit var booleanCaptor: ArgumentCaptor<Boolean> | |
lateinit var objectUnderTest: MyViewModel | |
@Before | |
fun setUp() { | |
MockitoAnnotations.initMocks(this) | |
// initMocks(this) | |
// ... | |
objectUnderTest = MyViewModel(activityHandlerMock) | |
} | |
@Test | |
fun thatNavigatesToAddListScreenOnAddClicked(){ | |
//given | |
//when | |
objectUnderTest.addNewList() | |
//then | |
Mockito.verify(activityHandlerMock).navigateTo(classCaptor.capture(), booleanCaptor.capture()) | |
var clazz = classCaptor.value | |
Assert.assertNotNull(clazz); | |
Assert.assertFalse(booleanCaptor.value); | |
} | |
} | |
class MyViewModel(val handler: IActivityHandler) { | |
fun addNewList() { | |
handler.navigateTo(Activity::class.java, false) | |
} | |
} | |
interface IActivityHandler { | |
fun navigateTo(capture: Class<Activity>?, capture1: Boolean) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment