Skip to content

Instantly share code, notes, and snippets.

@alwarren
Last active September 24, 2018 23:14
Show Gist options
  • Select an option

  • Save alwarren/333bf04b98e1001c55c5d157cf657a26 to your computer and use it in GitHub Desktop.

Select an option

Save alwarren/333bf04b98e1001c55c5d157cf657a26 to your computer and use it in GitHub Desktop.
Integration Testing MVP With Mockito - Kotlin
/**
* An MVP integration test
*
* This test verifies integration of all three MVP components without actually implementing
* any of those components in the application.
*
* Passing on 9/24/2018
*/
class MainPresenterTest {
private val repository: Repository = mock(Repository::class.java)
private val view: MainView = mock(MainView::class.java)
private val manyItems = Arrays.asList(Item(), Item())
private val noItems = Collections.emptyList<Item>()
private lateinit var presenter: MainPresenter
@Before
fun setup() {
// Pass the trampoline scheduler to the observer for testing
presenter = MainPresenter(view, repository, Schedulers.trampoline())
// Change subscriber scheduler to match observer for testing
RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
}
@After
fun cleanUp() {
RxJavaPlugins.reset()
}
@Test
fun shouldPassItemsToView() {
`when`(repository.getItems())
.thenReturn(Single.just(manyItems))
presenter.loadItems()
verify(view).displayItems(manyItems)
}
@Test
fun shouldPassNoItemsToView() {
`when`(repository.getItems())
.thenReturn(Single.just(noItems))
presenter.loadItems()
verify(view).displayNoItems()
}
@Test
fun shouldHandleError() {
`when`(repository.getItems())
.thenReturn(Single.error { Throwable("fail") })
presenter.loadItems()
verify(view).displayError()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment