用robolectric测试烤面包

时间:2015-01-20 17:20:07

标签: android robolectric

使用Robolectric执行测试时,我的BroadcastReceiver中的

onReceive永远不会调用。

测试

@Test
public void WhenSentNewMessageInAirplaneMode_ToastShouldBeDisplayed() {
    // Arrange
    ShadowSettings.setAirplaneMode(true);
    ImageButton sendBtn = (ImageButton)activity.findViewById(R.id.sendBtn);
    EditText messageEditText = (EditText)activity.findViewById(R.id.messageEditText);
    EditText recipientsEditText = (EditText)activity.findViewById(R.id.recipientsEditText);

    messageEditText.setText("<a message>");
    recipientsEditText.setText("<a address>");

    // Act
    sendBtn.performClick();

    // Assert
    assertEquals(Robolectric.application.getString(R.string.message_not_sent), ShadowToast.getTextOfLatestToast());  // --> ShadowToast.getTextOfLatestToast() is always null
}

我的活动

OnCreate

PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SmsMessageSender.SENT), PendingIntent.FLAG_UPDATE_CURRENT);

覆盖

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(smsSentReceiver);
}

@Override
protected void onResume() {
    super.onResume(); // This call always
    smsSentReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            if (getResultCode() != Activity.RESULT_OK) {
                Toast.makeText(getBaseContext(), getString(R.string.message_not_sent), Toast.LENGTH_LONG).show(); // On test this never call
            }
        }
    };

    registerReceiver(smsSentReceiver, new IntentFilter(SmsMessageSender.SENT));
}

点击发送btn

smsManager.sendMultipartTextMessage(recipient, null, messageParts,
                new ArrayList<>(Arrays.asList(sentPI))
                , null)
        );

1 个答案:

答案 0 :(得分:2)

我认为这与你如何注册广播接收器有关。如果您正在收听LocalBroadcast,您可以像这样实现它们:

LocalBroadcastActivity

package be.acuzio.mrta;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import be.acuzio.mrta.R;
import be.acuzio.mrta.util.Logger;

public class LocalBroadcastActivity extends Activity {
    final private BroadcastReceiver myReceiver =new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Logger.d(TAG, "onReceive called");

            Toast.makeText(context, "onReceive Toast Called", Toast.LENGTH_SHORT).show();
        }
    };

    private final static String TAG = LocalBroadcastActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_broadcast);
    }

    @Override
    protected void onPause() {
        super.onPause();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(this.myReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();

        LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver, new IntentFilter("be.acuzio.mrta.LOCAL_BROADCAST"));
    }
}

LocalBroadcastActivityTest

在测试中,请确保构建活动并触发简历。

package be.acuzio.mrta.test;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.shadows.ShadowToast;

import java.util.Iterator;
import java.util.List;

import be.acuzio.mrta.LocalBroadcastActivity;
import be.acuzio.mrta.MainActivity;
import be.acuzio.mrta.receiver.MyBroadcastReceiver;


@Config(emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class LocalBroadcastActivityTest {
    @Before
    public void setup() {
        //do whatever is necessary before every test
    }

    @Test
    public void testActivityFound() {
        Activity activity = Robolectric.buildActivity(LocalBroadcastActivity.class).create().get();

        Assert.assertNotNull(activity);
    }

    @Test
    public void testBroadcastReceiverRegistered() {
        Activity activity = Robolectric.buildActivity(LocalBroadcastActivity.class).create().resume().get();

        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(activity);

        localBroadcastManager.sendBroadcast(new Intent("be.acuzio.mrta.LOCAL_BROADCAST"));

        Assert.assertEquals("onReceive Toast Called", ShadowToast.getTextOfLatestToast());

    }
}

希望这有帮助!

注意:尝试使您的代码可以测试:尝试在不同的文件/类中拆分不同的“功能”,您可以单独测试。