如何测试广播接收器是在单元测试中执行的?

时间:2015-12-11 04:25:16

标签: android unit-testing

我需要在主线程上调用一个方法,为此我正在使用@UiThreadTest并在我的测试中调用方法。 该方法负责发送广播接收器。 所以我正在注册接收器并设置一个布尔值来测试广播是否已被发送。 在日志中,我能够看到广播被执行。 但是在测试中获得断言失败错误。 这是一些线程问题吗?

// broadcase receiver

private class Receiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, final Intent intent) {
       if (mUiUpdateReceiver != null && intent.getAction().equals(MessageCenter.INTENT_UI_REFRESH)) {
           mbroadcastFlag = true;
       }
    }
}

//测试

@UiThreadTest
    public void test_broadcast () throws Exception{
        mUiUpdateReceiver = new Receiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(SampleClass.INTENT_REFRESH);

        LocalBroadcastManager.getInstance(mAppContext).registerReceiver(mUiUpdateReceiver, intentFilter);

        mbroadcastFlag = false;
        sampleClass.method();
        Thread.sleep(2000);
        assertTrue(mbroadcastFlag);
        LocalBroadcastManager.getInstance(mAppContext).unregisterReceiver(mUiUpdateReceiver);

    }

2 个答案:

答案 0 :(得分:1)

Konstantin Loginov的answear还可以,但是它使用了JUnit3(因为他扩展了ApplicationTestCase;如果你扩展任何TestCase类,Android框架将默认使用JUnit3 runner来运行你的测试。)

如果你想使用更新的JUnit 4你可以使用这样的东西:

 public class MyReceiver extends BroadcastReceiver {
 boolean flag;

    @Override
    public void onReceive(Context context, Intent intent) {
        Uri packageName = intent.getData();
        if(packageName.toString().equals("package:" + context.getPackageName())){
            flag = true;
        }
    }
}

测试本身:

public class MyReceiverTest {

    Context context;


    @Before
    public void init() {
        context = new RenamingDelegatingContext(InstrumentationRegistry.getInstrumentation().getTargetContext(), "test_");
    }

    @Test
    public void myReceiverTest() throws InterruptedException {

        MyReceiver receiver = new MyReceiver();

    //remeber to change Intent.ACTION_PACKAGE_REPLACED 
//with your action name from your mainfest file
        LocalBroadcastManager.getInstance(context).registerReceiver(receiver, new IntentFilter(Intent.ACTION_PACKAGE_REPLACED));
        String pn = context.getPackageName();



        Intent i = new Intent(Intent.ACTION_PACKAGE_REPLACED);
        i.setData(Uri.parse("package:" + context.getPackageName()));
        LocalBroadcastManager.getInstance(context).sendBroadcast(i);
        Thread.sleep(2000);
        boolean b = receiver.flag;
        assertTrue(b);

        LocalBroadcastManager.getInstance(context).unregisterReceiver(receiver);
        assertTrue(true);


    }

答案 1 :(得分:0)

我为你写了一个小型的hello-world,带有广播接收器和单元测试。

这是我的广播接收器:

public class UiRefreshReceiver extends BroadcastReceiver {

    boolean broadcastFlag;

    public UiRefreshReceiver() {}

    @Override
    public void onReceive(Context context, Intent intent) {
        broadcastFlag = true;
    }
}

为它测试课程:

public class ApplicationTest extends ApplicationTestCase<Application> {
    public ApplicationTest() {
        super(Application.class);
    }

    @UiThreadTest
    public void test_broadcast () throws Exception{
        UiRefreshReceiver mUiUpdateReceiver = new UiRefreshReceiver();
        LocalBroadcastManager.getInstance(getContext()).registerReceiver(mUiUpdateReceiver, new IntentFilter("my-event"));

        assertFalse(mUiUpdateReceiver.broadcastFlag);

        LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("my-event"));
        Thread.sleep(2000);

        assertTrue(mUiUpdateReceiver.broadcastFlag);

        LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mUiUpdateReceiver);

    }
}

我希望,这有帮助。

虽然,在我的拙见中 - 只需对onReceive()代码进行单元测试就足够了。