Skip to content

Instantly share code, notes, and snippets.

@varshabhatia007
Created May 17, 2019 09:17
Show Gist options
  • Save varshabhatia007/c5fabc269f1113446c35568471a0c7e9 to your computer and use it in GitHub Desktop.
Save varshabhatia007/c5fabc269f1113446c35568471a0c7e9 to your computer and use it in GitHub Desktop.
@Mock
private lateinit var repo: CustomerRepo
private lateinit var customerUsecase: CustomerUsecase
@Before
fun setUp() {
customerUsecase = CustomerInteractor(repo)
}
@Test
fun `should return customer data`() {
val expectedCustomerData = mock(CustomerDetail::class.java)
`when`(repo.fetchCustomerData()).thenReturn(Single.just(expectedCustomerData))
customerUsecase.getCustomerData()
.test()
.assertComplete()
.assertNoErrors()
.assertValueCount(1)// Assert that this TestObserver/TestSubscriber received the specified number onNext events.
.assertValue {
assertEquals(expectedCustomerData, it)
true
}
verify(repo).fetchCustomerDetail()
}
@Test
fun `should handle error for customer`() {
val body = ResponseBody.create(MediaType.parse("application/json"), "Something went wrong")
val expectedResponse = Response.error<Response>(500, body)
`when`(repo.fetchCustomerDetail()).thenReturn(Single.error(HttpException(expectedResponse)))
customerUsecase
.getCustomerData()
.test()
.assertValueCount(0)
.assertError {
it is HttpException
}
verify(repo).fetchCustomerDatail()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment