Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

checkCondition call after return stub response #9

Open
alexei-28 opened this issue May 8, 2019 · 0 comments
Open

checkCondition call after return stub response #9

alexei-28 opened this issue May 8, 2019 · 0 comments

Comments

@alexei-28
Copy link

alexei-28 commented May 8, 2019

Adnroid Studio 3.3.

In my app I use Retrofit to do http request. Also I use MockWebServer to return stub response.
On activity when I click button I start async (call.enqueue(callback);) http request (by Retrofit 2) and wait callback method to return response. Here Espresso's test:

@RunWith(AndroidJUnit4::class)
class AddTraderActivityTest {
@get:Rule
    var addTraderIntentTestRule: IntentsTestRule<AddTraderActivity> = IntentsTestRule(AddTraderActivity::class.java)

private val baseEditText = viewWithId(baseTextInputEditText)
    private val quoteEditText = viewWithId(quoteTextInputEditText)
    private val buttonStart = viewWithId(startButton)

    @Before
    fun setup() {
        mockServer = MockWebServer()
        mockServer.start(8081)
        Debug.d(TAG, "SUCCCESS_START_MOCKWEBSRVER")
    }

  @Test
fun buttonStart_click_longResponse() {
    // stub response
    mockServer.enqueue(MockResponse()
            .setResponseCode(200)
            .setBody(FileUtil.getStringFromFile(context, "add_trader_success_200.json"))
            .setBodyDelay(5000, TimeUnit.MILLISECONDS))

    onView(withId(R.id.baseTextInputEditText))
            .perform(typeText(BASE_TEST))
    onView(withId(R.id.quoteTextInputEditText))
            .perform(typeText(QUOTE_TEST))
    onView(withId(R.id.startButton))
            .perform(click())
    onView(withText(R.id.containerProgressBarLayout))
            .check(matches(isDisplayed()))
}

But problem is when execute perform(click()) the method check is not call until not get stub response (after 5 seconds).
But I need call method check immediately after perform(click()) method. Because I need to check is containerProgressBarLayout is isDisplayed() while not return stub response. I need to check my view DURING loading data.
So to do this I try this:

   @Test
    fun buttonStart_click_longResponse() {
        // stub response
        mockServer.enqueue(MockResponse()
                .setResponseCode(200)
                .setBody(FileUtil.getStringFromFile(context, "add_trader_success_200.json"))
                .setBodyDelay(5000, TimeUnit.MILLISECONDS))

        baseEditText.type(BASE_TEST)
        quoteEditText.type(QUOTE_TEST)
        buttonStart.click()
        Debug.d(TAG, "AFTER_CLICK")
        ConditionWatcher.waitForCondition(object : Instruction() {
            override fun getDescription(): String {
                return "test description"
            }

            override fun checkCondition(): Boolean {
                Debug.d(TAG, "checkCondition")
                return true
            }
        })
        onView(withText(containerProgressBarLayout))
                .check(matches(isDisplayed()))
		}

		

But log AFTER_CLICK and method checkCondition() call AFTER 5 SECONDS.

When call click then call this production code:

TransportService.executeTraderOperation(Trader.Operation.CREATE, base.trim(), quote.trim(), new DefaultRestClientCallback<Void>() {
            @Override
            public void onSuccess(Response<Void> response) {
            }

            @Override
            public void onError(ErrorResponse errorResponse) {
             
            }
        });
    }

and this:

public static void executeTraderOperation(Trader.Operation traderOperation, String base, String quote, Callback<Void> callback) {
        TraderMonitorRestClient traderMonitorRestClient = RestClientFactory.createRestClient(TraderMonitorRestClient.class);
        String sender = BuildConfig.APPLICATION_ID + "_" + BuildConfig.VERSION_NAME;
        String key = DateUtil.getDateAsString(new Date(), "mmHHddMMyyyy");
        Call<Void> call = traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base, quote, sender, key);
        // asynchronously
        call.enqueue(callback);
    }

fun ViewInteraction.click(): ViewInteraction = perform(ViewActions.click())

fun viewWithId(@IdRes resId: Int): ViewInteraction {
    return onView(withId(resId))
}
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant