处理程序未在Instrumentation Test中执行Runnable

时间:2018-07-25 10:04:02

标签: java android testing android-service android-handler

我编写了一个Android工具测试,该测试会调用我的服务并通过广播接收答案。

与服务对话的要测试的代码使用处理程序。

在测试我的测试过程中,^^我注意到处理程序的行为不符合预期。因此,我编写了一个测试来检查这种行为:

import android.os.Handler;
import android.support.test.annotation.UiThreadTest;

import org.junit.Assert;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@RunWith(AndroidJUnit4.class)
public class HandlerTest {

    private CountDownLatch countDownLatch;

    @Test
    @UiThreadTest
    public void handlerTest() {

        final Handler handler = new Handler();
        countDownLatch = new CountDownLatch(1);

        final Runnable r = new Runnable() {
            @Override
            public void run() {
                // this code gets not executed
                countDownLatch.countDown();
            }
        };

        handler.postDelayed(r, 1000);

        try {
            final boolean finishedWithoutTimeout
                    = countDownLatch.await(5, TimeUnit.SECONDS);
            Assert.assertTrue(finishedWithoutTimeout);

        } catch (final InterruptedException e) {
            e.printStackTrace();
        }
    }
}

处理程序不执行可运行的代码。这也是我的生产代码的问题。

我用Looper.prepare()批注解决了@UiThreadTest的问题。

关于我的处理程序问题有什么建议吗?

1 个答案:

答案 0 :(得分:0)

原因是您在此处锁定了thread

final boolean finishedWithoutTimeout = countDownLatch.await(5, TimeUnit.SECONDS);

thread被锁定后,您将无法与rannable一起发送handlerThread刚刚被锁定。 您可以通过将handler链接到另一个thread来解决此问题。这是通过handlerThread的简单解决方案:

@Test
@UiThreadTest
public void handlerTest() {
    countDownLatch = new CountDownLatch(1);
    final HandlerThread handlerThread = new HandlerThread("solution!");
    handlerThread.start();
    final Runnable r = new Runnable() {
        @Override
        public void run() {
            countDownLatch.countDown();
        }
    };
    Handler handler = new Handler(handlerThread.getLooper());
    handler.postDelayed(r, 1000);
    try {
        final boolean finishedWithoutTimeout = countDownLatch.await(5, TimeUnit.SECONDS);
        Assert.assertTrue(finishedWithoutTimeout);
    } catch (final InterruptedException e) {
        e.printStackTrace();
    }
}