Last active
December 17, 2017 23:43
-
-
Save ShaishavGandhi/db4513d11dda5bf0a14d9ca879e27130 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
@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