Created
May 23, 2021 12:42
-
-
Save ClaudeHangui/997b1b5a2eb4b46114b06d5aea3a2f01 to your computer and use it in GitHub Desktop.
Sample demo of an Integration test
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
package com.changui.penguinpay | |
import android.os.SystemClock.sleep | |
import androidx.arch.core.executor.testing.InstantTaskExecutorRule | |
import androidx.test.espresso.Espresso.onData | |
import androidx.test.espresso.Espresso.onView | |
import androidx.test.espresso.IdlingRegistry | |
import androidx.test.espresso.action.ViewActions.* | |
import androidx.test.espresso.assertion.ViewAssertions.matches | |
import androidx.test.espresso.matcher.ViewMatchers.* | |
import androidx.test.ext.junit.rules.activityScenarioRule | |
import androidx.test.ext.junit.runners.AndroidJUnit4 | |
import androidx.test.filters.LargeTest | |
import androidx.test.rule.GrantPermissionRule | |
import com.jakewharton.espresso.OkHttp3IdlingResource | |
import okhttp3.OkHttpClient | |
import okhttp3.mockwebserver.Dispatcher | |
import okhttp3.mockwebserver.MockResponse | |
import okhttp3.mockwebserver.MockWebServer | |
import okhttp3.mockwebserver.RecordedRequest | |
import org.hamcrest.Matchers.allOf | |
import org.hamcrest.core.Is.`is` | |
import org.hamcrest.core.IsInstanceOf.instanceOf | |
import org.hamcrest.core.StringContains.containsString | |
import org.junit.After | |
import org.junit.Rule | |
import org.junit.Test | |
import org.junit.rules.TestRule | |
import org.junit.runner.RunWith | |
import org.koin.test.KoinTest | |
@LargeTest | |
@RunWith(AndroidJUnit4::class) | |
class SendTransactionsFlowTest: KoinTest { | |
@get:Rule | |
val activityTestRule = activityScenarioRule<MainActivity>() | |
@get:Rule | |
var rule: TestRule = InstantTaskExecutorRule() | |
@get:Rule | |
var permissionRule: GrantPermissionRule = GrantPermissionRule.grant( | |
android.Manifest.permission.INTERNET, | |
android.Manifest.permission.ACCESS_NETWORK_STATE, | |
) | |
val mockWebServer: MockWebServer = MockWebServer() | |
@Test | |
fun verify_displaying_screen_to_send_a_transaction_all_user_interactions_are_made() { | |
mockWebServer.dispatcher = object : Dispatcher() { | |
override fun dispatch(request: RecordedRequest): MockResponse { | |
return when(request.path) { | |
"/latest.json?app_id=appId" -> "api.json".mockResponsePathOK() | |
else -> MockResponse().setResponseCode(404) | |
} | |
} | |
} | |
mockWebServer.start() | |
val idlingResource = OkHttp3IdlingResource.create("okhttp", OkHttpClient.Builder().build()) | |
IdlingRegistry.getInstance().register(idlingResource) | |
onView(withResourceName("recipient_phone_number")).check(matches(isDisplayed())) | |
onView(withText("You're sending money to")).check(matches(isDisplayed())) | |
onView(allOf(withResourceName("user_name"), withHint(R.string.hint_firstname_lastname))).check(matches(isDisplayed())) | |
onView(withResourceName("user_name")).perform(typeText("claude hangui"), closeSoftKeyboard()) | |
sleep(60) | |
onView(withResourceName("spinner")).perform(click()) | |
onData(allOf(`is`(instanceOf(String::class.java)), `is`("Nigeria"))).perform(click()) | |
onView(withResourceName("country_phone_prefix")).check(matches(withText("234"))) | |
onView(withResourceName("phone_digit_count")).check(matches(withText((containsString("7"))))) | |
onView(withResourceName("phone_digit_count")).check(matches(isDisplayed())) | |
onView(withResourceName("receiver_currency")).check(matches(withText("NGN"))) | |
onView(withResourceName("conversion_rate")).check(matches(isEnabled())) | |
sleep(60) | |
onView(withResourceName("conversion_rate")).check(matches(withText((containsString("NGN"))))) | |
onView(withResourceName("recipient_phone_number")).perform(typeText("7654321"), closeSoftKeyboard()) | |
sleep(60) | |
onView(withResourceName("amount_sent")).perform(typeText("11"), closeSoftKeyboard()) | |
onView(withResourceName("amount_received")).check(matches(withText((containsString("10011"))))) | |
IdlingRegistry.getInstance().unregister(idlingResource) | |
} | |
fun String.mockResponsePathOK(code: Int = 200): MockResponse { | |
return MockResponse().setResponseCode(code).setBody(ResourceReaderUtils.loadJsonResource(this)) | |
} | |
@After | |
fun tearDown() { | |
mockWebServer.shutdown() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment