7

如何创建 Android JUnit 测试用例来测试 Activity 中生成的 Intent 的内容?

我有一个包含 EditText 窗口的 Activity,当用户完成输入所需数据时,Activity 会向 IntentService 启动一个 Intent,该 IntentService 记录数据并继续应用程序进程。这是我要测试的类, OnEditorActionListener/PasscodeEditorListener 被创建为一个单独的类:

public class PasscodeActivity extends BaseActivity {
    EditText                    m_textEntry = null;
    PasscodeEditorListener      m_passcodeEditorListener = null;

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

        m_passcodeEditorListener = new PasscodeEditorListener();
        m_textEntry = (EditText) findViewById(R.id.passcode_activity_edit_text);
        m_textEntry.setTag(this);
        m_textEntry.setOnEditorActionListener(m_passcodeEditorListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        /*
         *   If we're covered for any reason during the passcode entry,
         *   exit the activity AND the application...
         */
        Intent finishApp = new Intent(this, CoreService.class);
        finishApp.setAction(AppConstants.INTENT_ACTION_ACTIVITY_REQUESTS_SERVICE_STOP);
        startService(finishApp);
        finish();
    }

}



class PasscodeEditorListener implements OnEditorActionListener{
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        PasscodeActivity activity = (PasscodeActivity) v.getTag();
        boolean imeSaysGo = ((actionId & EditorInfo.IME_ACTION_DONE)!=0)?true:false;
        boolean keycodeSaysGo = ((null != event) && 
                (KeyEvent.ACTION_DOWN == event.getAction()) && 
                (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))?true:false;

        if (imeSaysGo || keycodeSaysGo){
            CharSequence seq = v.getText();
            Intent guidEntry = new Intent(activity, CoreService.class);
            guidEntry.setAction(AppConstants.INTENT_ACTION_PASSCODE_INPUT);
            guidEntry.putExtra(AppConstants.EXTRA_KEY_GUID, seq.toString());
            activity.startService(guidEntry);
            return true;
        }
        return false;
    }
}

如何拦截活动生成的两个可能的出站 Intent 并验证其内容?

谢谢

4

2 回答 2

6

我想出了如何在另一个网站的帮助下使用 ContextWrapper。

使用 ContextWrapper 并覆盖所有意图函数。概括我所有的 Activity 测试,我扩展了 ActivityUnitTestCase 类并将解决方案实现为 shim。享受:

import android.app.Activity;
import android.app.Instrumentation;
import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.test.ActivityUnitTestCase;

public class IntentCatchingActivityUnitTestCase<T extends Activity> extends ActivityUnitTestCase<T> {

    protected Activity m_activity;
    protected Instrumentation m_inst;
    protected Intent[] m_caughtIntents;
    protected IntentCatchingContext m_contextWrapper;

    protected class IntentCatchingContext extends ContextWrapper {
        public IntentCatchingContext(Context base) {
            super(base);
        }

        @Override
        public ComponentName startService(Intent service) {
            m_caughtIntents = new Intent[] { service };
            return service.getComponent();
        }

        @Override
        public void startActivities(Intent[] intents) {
            m_caughtIntents = intents;
            super.startActivities(intents);
        }

        @Override
        public void startActivity(Intent intent) {
            m_caughtIntents = new Intent[] { intent };
            super.startActivity(intent);
        }

        @Override
        public boolean stopService(Intent intent) {
            m_caughtIntents = new Intent[] { intent };
            return super.stopService(intent);
        }
    }

    // --//
    public IntentCatchingActivityUnitTestCase(Class<T> activityClass) {
        super(activityClass);
    }

    protected void setUp() throws Exception {
        super.setUp();
        m_contextWrapper = new IntentCatchingContext(getInstrumentation().getTargetContext());
        setActivityContext(m_contextWrapper);
        startActivity(new Intent(), null, null);
        m_inst = getInstrumentation();
        m_activity = getActivity();
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

}
于 2012-04-27T13:10:36.513 回答
1

或者,您可以重构代码以进行“干净”的单元测试(我的意思是一个单元测试,除了被测类之外,所有的东西都被模拟了)。实际上,我自己也遇到了一种情况,java.lang.RuntimeException: Stub!因为我要进行单元测试的代码创建了包含我注入的模拟的新意图。

我考虑为意图创建自己的工厂。然后我可以向我的测试类注入一个模拟工厂:

public class MyClassToBeTested {
    public MyClassToBeTested(IntentFactory intentFactory) {
        //assign intentFactory to field
    }
    ....
    public void myMethodToTestUsingIntents() {
        Intent i = intentFactory.create();
        i.setAction(AppConstants.INTENT_ACTION_PASSCODE_INPUT);
        //when doing unit test, inject a mocked version of the
        //IntentFactory and do the necessary verification afterwards.
        ....
    }
}

我的情况和你的不一样,但我相信你也可以应用工厂模式来解决它。我更喜欢编写代码来支持真正的单元测试,但必须承认您的解决方案非常聪明。

于 2013-08-27T07:57:24.340 回答