Skip to content

Instantly share code, notes, and snippets.

@CyxouD
Last active April 8, 2019 12:18
Show Gist options
  • Save CyxouD/3780500fd6126ae9b98f00a58d776b34 to your computer and use it in GitHub Desktop.
Save CyxouD/3780500fd6126ae9b98f00a58d776b34 to your computer and use it in GitHub Desktop.
Examples of tests for: 1) testing MVP architecture (Model-View-Presenter Communication.kt) - tests communication between view and presenter and mocking repository behavior; 2) Repository (Repository.kt) - tests how data from local data source and remote data source is received and processed; 3) UI (UI (Integration).kt) - tests UI behavior
//imports are missing
class CardsListPresenterTest {
private lateinit var presenter: CardsListPresenter
private val view = mock<CardsListContact.View>()
private val repository = mock<Repository>()
private val scheduleProvider = ImmediateSchedulerProvider()
// ... some other tests
@Test
fun createPresenter_setsPresenterToView() {
presenter = CardsListPresenter(repository, view, scheduleProvider)
verify(view).setPresenter(presenter)
}
@Test
fun addCard_showAddCard() {
presenter = CardsListPresenter(repository, view, scheduleProvider)
presenter.addCard()
verify(view).showAddCard()
}
@Test
fun subscribe_showCardsIfCardsNotEmpty() {
presenter = CardsListPresenter(repository, view, scheduleProvider)
val notEmptyCreditCards = listOf(CreditCard())
`when`(repository.getCreditCards()).thenReturn(Single.just(ResultWithPossibleException(notEmptyCreditCards.toOptional())))
presenter.subscribe()
verify(view).showCards(notEmptyCreditCards)
assertEquals(true, presenter.currentCards == notEmptyCreditCards)
}
@Test
fun subscribe_showNoCardsIfCardsEmpty() {
presenter = CardsListPresenter(repository, view, scheduleProvider)
val emptyCreditCards = listOf<CreditCard>()
`when`(repository.getCreditCards()).thenReturn(Single.just(ResultWithPossibleException(emptyCreditCards.toOptional())))
presenter.subscribe()
verify(view).showNoCards()
assertEquals(true, presenter.currentCards.isEmpty())
}
@Test
fun deleteCard_showCardDeletedSuccessfully() {
presenter = CardsListPresenter(repository, view, scheduleProvider)
val someUUID = "someUUID"
val someCompanyIdentifier = "someCompanyIdentifier"
`when`(repository.deleteCreditCards(any()))
.thenReturn(Completable.complete())
presenter.deleteCreditCard(someUUID, someCompanyIdentifier)
verify(view).showCardDeletedSuccessfully(someUUID, someCompanyIdentifier)
}
@Test
fun deleteCard_showCardFailedToDelete() {
presenter = CardsListPresenter(repository, view, scheduleProvider)
val someUUID = "someUUID"
val someCompanyIdentifier = "someCompanyIdentifier"
`when`(repository.deleteCreditCards(any()))
.thenReturn(Completable.error(Exception()))
presenter.deleteCreditCard(someUUID, someCompanyIdentifier)
verify(view).showCardFailedToDelete()
}
// ... some other tests
}
//imports are missing
class RepositoryTest {
private lateinit var mRepository: Repository
private val mRemoteRepository = mock<DataSource>()
private val mLocalRepository = mock<DataSource>()
// ... some other tests
@Before
fun setupRepository() {
mRepository = Repository.getInstance(mRemoteRepository, mLocalRepository)
}
@After
fun destroyRepositoryInstance() {
Repository.destroyInstance()
}
@Test
fun authorize_SaveToCacheIfSuccess() {
val credentials = Credentials(null, null, null)
val someToken = "sometoken"
`when`(mRemoteRepository.authorize(credentials)).thenReturn(Flowable.just(someToken))
val testSubscriber = TestSubscriber<String>()
mRepository.authorize(credentials).subscribe(testSubscriber)
verify(mRemoteRepository).authorize(eq(credentials))
verify(mLocalRepository).saveToken(eq(someToken))
}
@Test
fun getConversationId_conversationIdIsRetrievedBothFromRemoteAndLocal() {
val localConvId = ""
val remoteConvId = ""
setConversationIdAvailable(mLocalRepository, localConvId)
setConversationIdAvailable(mRemoteRepository, remoteConvId)
mRepository.getConversationId()
verify(mLocalRepository).getConversationId()
verify(mRemoteRepository).getConversationId()
}
@Test
fun getConversationId_conversationIdRetrievedFromLocal_whenConvIdAvailableInLocalStorageAndRemoteStorageError() {
val localConvId = "local"
setConversationIdAvailable(mLocalRepository, localConvId)
setConversationIdNotAvailableError(mRemoteRepository)
val testSubscriberRemoteError = TestSubscriber<Optional<String>>()
mRepository.getConversationId().subscribe(testSubscriberRemoteError)
verify(mRemoteRepository).getConversationId()
verify(mLocalRepository).getConversationId()
testSubscriberRemoteError.assertValue(localConvId.toOptional())
}
// ... some other tests
}
//imports are missing
class CardsListScreenTest {
@Rule
@JvmField
var mIntentTestRule: IntentsTestRule<CardsListActivity> =
IntentsTestRule(CardsListActivity::class.java,
false,
false
)
// ... some other tests
@Before
fun startActivity() {
val startIntent = Intent()
mIntentTestRule.launchActivity(startIntent)
}
@After
fun tearDown() {
unregisterAllIdlingRegisterResources()
}
@Test
fun testThatThatAddEditCardActivityOpenedAfterPressingAdd() {
onView(withId(R.id.ic_add)).perform(ViewActions.click())
Intents.intended(IntentMatchers.hasComponent(AddEditCardActivity::class.java.name))
}
@Test
fun testThatThatEmptyScreenAndNotListIsShownByDefault() {
onView(withId(R.id.cards_list)).check(matches(not(isDisplayed())))
onView(withId(R.id.empty_view)).check(matches(isDisplayed()))
}
// ... some other tests
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment