Skip to content

Instantly share code, notes, and snippets.

@ShaishavGandhi
Last active December 17, 2017 23:43
Show Gist options
  • Save ShaishavGandhi/db4513d11dda5bf0a14d9ca879e27130 to your computer and use it in GitHub Desktop.
Save ShaishavGandhi/db4513d11dda5bf0a14d9ca879e27130 to your computer and use it in GitHub Desktop.
@RunWith(JUnit4::class)
class BlogPresenterUTest {
lateinit var blogRepository : BlogRepository
lateinit var blogView : BlogView
lateinit var blogPresenter : BlogPresenter
@Before @Throws fun setUp(){
RxAndroidPlugins.setInitMainThreadSchedulerHandler({ Schedulers.trampoline()})
MockitoAnnotations.initMocks(this)
blogPresenter = BlogPresenter(blogRepository = blogRepository, view = blogView)
}
@Test fun testBlogsReturnsList() {
val blogs = getMockedBlogs(12)
`when`(blogRepository.blogs()).thenReturn(Observable.just(blogs))
blogPresenter.blogs()
// Verify view method is called
verify(view).setBlogs(blogs)
}
@Test fun testBlogsReturnsError() {
`when`(blogRepository.blogs()).thenReturn(Observable.error(NetworkErrorException()))
blogPresenter.blogs()
// View is never called
verify(view, never()).setBlogs(any())
}
fun getMockedBlogs(count : Int) : List<Blog> {
val blogs = ArrayList<Blog>()
for (i in 0..count) {
val blog = mock(Blog::class.java)
blogs.add(blog)
}
return blogs
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment