无法收到存根响应但无法检查视图

时间:2019-05-08 14:57:10

标签: android-espresso ui-testing mockwebserver

我有一个带有2个编辑文本和按钮的活动。当单击按钮时,我通过Retrofit启动 async http请求(调用async方法enqueue()),然后等待回调的响应。 为了模拟长响应(例如5秒),我使用MockWebServerhttps://github.com/square/okhttp/tree/master/mockwebserver)。

在执行HTTP请求期间,显示进度条。获得Retrofit的回调响应后,进度条将隐藏。 很好。

所以我需要检查http请求正在执行(未收到响应)时,进度条是否显示。

这里是意式浓缩咖啡的测试:

@RunWith(AndroidJUnit4::class)
class AddTraderActivityTest {
    private val context = InstrumentationRegistry.getInstrumentation().getContext()
    private val targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext()
    private lateinit var mockServer: MockWebServer
    @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)
     }

    @After
    @Throws
    fun tearDown() {
         mockServer.shutdown()
}

@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")
        onView(withText(containerProgressBarLayout)).check(matches(isDisplayed()))
}

fun viewWithId(@IdRes resId: Int): ViewInteraction {
    return onView(withId(resId))
}

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

方法buttonStart.click()调用下一个生产代码:

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

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

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);
    }

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/jsonViewToolBar"
            layout="@layout/tool_bar"
            android:title='@{@string/add_trader}'
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/baseTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/default_margin"
            android:layout_marginEnd="@dimen/half_default_margin"
            android:text="@string/base"
            android:textSize="@dimen/text_view_text_size"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/jsonViewToolBar" />

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/baseTextInputEditText"
            style="@style/textViewOneLine"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="@dimen/edit_text_text_size"
            app:layout_constraintEnd_toEndOf="@+id/baseTextView"
            app:layout_constraintStart_toStartOf="@+id/baseTextView"
            app:layout_constraintTop_toBottomOf="@+id/baseTextView" />

        <TextView
            android:id="@+id/quoteTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/default_margin"
            android:text="@string/quote"
            android:textSize="@dimen/text_view_text_size"
            app:layout_constraintEnd_toEndOf="@+id/baseTextView"
            app:layout_constraintStart_toStartOf="@+id/baseTextView"
            app:layout_constraintTop_toBottomOf="@+id/baseTextInputEditText" />

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/quoteTextInputEditText"
            style="@style/textViewOneLine"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="@dimen/edit_text_text_size"
            app:layout_constraintEnd_toEndOf="@+id/baseTextView"
            app:layout_constraintStart_toStartOf="@+id/baseTextView"
            app:layout_constraintTop_toBottomOf="@+id/quoteTextView" />


        <com.google.android.material.button.MaterialButton
            android:id="@+id/startButton"
            android:layout_width="0dp"
            android:layout_height="@dimen/min_height"
            android:layout_marginTop="@dimen/default_margin"
            android:layout_marginBottom="@dimen/default_margin"
            android:gravity="center"
            android:onClick="@{() -> handler.onClickStart()}"
            android:text="@string/start"
            android:textSize="@dimen/tool_bar_text_size"
            app:layout_constraintEnd_toEndOf="@+id/baseTextView"
            app:layout_constraintStart_toStartOf="@+id/baseTextView"
            app:layout_constraintTop_toBottomOf="@+id/quoteTextInputEditText"
            app:textAllCaps="true" />

        <include
            layout="@layout/progress_bar_layout"
            android:visibility="gone" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

此处progress_bar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/containerProgressBarLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#4777"
    android:clickable="true"
    android:elevation="2dp"
    android:focusable="true">

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="@dimen/min_height"
        android:layout_height="@dimen/min_height"
        android:indeterminateTint="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

但是此测试失败是因为:

  1. 呼叫buttonStart.click()

  2. 显示进度栏

  3. 等待5秒

  4. 获取存根响应

  5. 隐藏进度栏

  6. 在日志“ AFTER_CLICK”中打印并调用 onView(withText(containerProgressBarLayout)).check(matches(isDisplayed()))

结果测试失败,因为调用onView(withText(containerProgressBarLayout)).check(matches(isDisplayed()))时进度条已经隐藏。结果检查失败。

那么我如何查看进度条是否显示却未收到存根响应?

0 个答案:

没有答案