2

アプリで RxJava と EventBus を使用していて、テストが失敗することがあるため、アイドリング リソースを使用したいと考えています (同期が原因だと考えています)。

依存関係:

androidTestCompile 'com.android.support.test:runner:0.4'
androidTestCompile 'com.android.support.test:rules:0.4'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.1') {
    exclude group: 'com.android.support', module: 'appcompat'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude module: 'recyclerview-v7'
}
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'com.squareup.retrofit:retrofit-mock:1.9.0'
androidTestCompile 'com.squareup.assertj:assertj-android:1.1.0'
androidTestCompile 'com.squareup.spoon:spoon-client:1.2.0'

テスト失敗:

@RunWith(AndroidJUnit4.class)
@SmallTest
public class PushNotificationTests {

    @Test
    public void testSomething(){

        // do things...
        MyIdlingResource eventBusIdlingResource = new MyIdlingResource();
        registerIdlingResources(eventBusIdlingResource);

        Order.Status status = Order.getCurrentObservable().toBlocking().first().getStatus();
        assertThat(status).isEqualTo(Order.Status.PROGRESS);   // Fail,

        unregisterIdlingResources(eventBusIdlingResource)
    }

}

試してみるがうまくいかない:

@RunWith(AndroidJUnit4.class)
@SmallTest
public class PushNotificationTests {

    @Test
    public void testSomething(){

        // do things...

        MyIdlingResource eventBusIdlingResource = new MyIdlingResource();
        registerIdlingResources(eventBusIdlingResource);

        // Wait manually?
        int sleeps = 0;
        while(!eventBusIdlingResource.isIdleNow() || sleeps < 10){
            sleep(100);
            sleeps++;
        }

        Order.Status status = Order.getCurrentObservable().toBlocking().first().getStatus();
        assertThat(status).isEqualTo(Order.Status.PROGRESS);   // Fail,

        unregisterIdlingResources(eventBusIdlingResource)
    }

}
4

3 に答える 3

1

クラス内に sleep メソッドをカプセル化することになります。

import android.support.test.espresso.IdlingResource;

/**
 * Have functions to sleep the processor because assertions are not linked to
 * {@link IdlingResource} to do assertions, so should be used before asserts if there's an
 * idle process.
 */
public class IdlingResourceSleeper {

    private static final int SLEEPS_LIMIT = 50;
    private static final int SLEEPS_TIME = 10;

    /**
     * Used to sleep {@link IdlingResourceSleeper#SLEEPS_LIMIT} times and
     * {@link IdlingResourceSleeper#SLEEPS_TIME} ms until idlingResource.isIdleNow() is false.
     *
     * @param idlingResource
     */
    public static void sleep(IdlingResource idlingResource) {
        int sleeps = 0;
        while (!idlingResource.isIdleNow() || sleeps < SLEEPS_LIMIT) {
            android.os.SystemClock.sleep(SLEEPS_TIME);
            sleeps++;
        }

    }
}

ソース: -要旨 -投稿

于 2015-10-07T17:40:25.503 に答える