Skip to content

Instantly share code, notes, and snippets.

@markchristopherng
Created February 19, 2020 03:23
Show Gist options
  • Save markchristopherng/384cd447131301839cfa4ec8b9636a86 to your computer and use it in GitHub Desktop.
Save markchristopherng/384cd447131301839cfa4ec8b9636a86 to your computer and use it in GitHub Desktop.
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [Build.VERSION_CODES.P])
class GreetingServiceRoboTest {
@get:Rule
val mockitoRule: MockitoRule = MockitoJUnit.rule()
@Mock
lateinit var timeService: TimeService
@Test
fun testGreetingInTheMorning() {
val testModule = module {
bind<TimeService>().toInstance(object : TimeService {
override fun getHourOfDay(): Int = 9
})
}
KTP.openRootScope().installTestModules(testModule)
val controller = Robolectric.buildActivity(MainActivity::class.java).setup()
val textView = controller.get().findViewById<TextView>(R.id.greetingsTextView)
Assert.assertEquals("Good morning, welcome to Toothpick", textView.text)
Toothpick.reset()
}
@Test
fun testGreetingInTheEvening() {
val testModule = module {
bind<TimeService>().toInstance(timeService)
}
whenever(timeService.getHourOfDay()).thenReturn(18)
KTP.openRootScope().installTestModules(testModule)
val controller = Robolectric.buildActivity(MainActivity::class.java).setup()
val textView = controller.get().findViewById<TextView>(R.id.greetingsTextView)
Assert.assertEquals("Good evening, welcome to Toothpick", textView.text)
Toothpick.reset()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment