如何使用单元测试来测试一个按钮,启动了正确的活动 [英] How to use unit testing to test that a button launches the correct Activity

查看:206
本文介绍了如何使用单元测试来测试一个按钮,启动了正确的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个单元测试,将测试如果一个按钮启动被点击时正确的活动,但我有麻烦找出正确的code使用。 该testButton方法不运行,通过测试,但它随后导致错误:

  [2012-02-29十六点18分08秒 -  TroubleShootingAppTest]试运行失败:仪器仪表运行失败,原因是java.lang.RuntimeException的
 

测试那么早结束。任何人都可以请指教一下正确的code应该是什么?

这是我有:

 包com.integral.troubleshooter.test;

进口com.integral.troubleshooter.Question;
进口com.integral.troubleshooter.R;
进口com.integral.troubleshooter.TroubleShooterActivity;

进口android.app.Activity;
进口android.content.Intent;
进口android.test.ActivityInstrumentationTestCase2;
进口android.text.Layout;
进口android.view.View;
进口android.widget.Button;
进口android.widget.ImageView;
进口android.widget.Spinner;
进口android.widget.TextView;

公共类TroubleShooterActivityTest扩展
    ActivityInstrumentationTestCase2< TroubleShooterActivity> {

私人TroubleShooterActivity mActivity;
私人按钮mButton;
私人TextView的mTextView;
私人ImageView的mImageView;
私人字符串资源字符串;
私人活动nextActivity;

公共TroubleShooterActivityTest(){
    超(com.integral.troubleshooter.TroubleShooterActivity,TroubleShooterActivity.class);
}

 / *
 *设定每个测试之前测试环境。
 * @see android.test.ActivityInstrumentationTestCase2#设置()
 * /
@覆盖
保护无效设置()抛出异常{

    super.setUp();

    setActivityInitialTouchMode(假);


    mActivity = getActivity();

    mButton =(按钮)mActivity.findViewById(R.id.troubleShooter);

    mTextView =(TextView中)mActivity.findViewById(R.id.title);

    mImageView =(ImageView的)mActivity.findViewById(R.id.IntegralLogo);

    资源字符串= mActivity.getString(R.string.CustomerSupport);
}

公共无效测试preconditions(){
    assertTrue(mTextView!= NULL);
    assertTrue(mImageView!= NULL);
}

公共无效testText(){
    的assertEquals(资源字符串(字符串)mTextView.getText());
}

公共无效testButton(){
     mActivity.runOnUiThread(
                新的Runnable(){
                    公共无效的run(){
                       mButton.performClick();
                       nextActivity = getActivity();
                       的assertEquals(nextActivity,Question.class);
                    }
                }
            );



}
}
 

解决方案

试着检查仪器不是present。运行此命令会以项目的环境present仪器的清单:

  $亚行外壳时列表仪器
 

我希望这帮助。

I am trying to write a unit test that will test if a button launches the correct activity when it is clicked, but I am having trouble figuring out the correct code to use. The testButton method does run and pass the tests, however it then causes an error:

[2012-02-29 16:18:08 - TroubleShootingAppTest] Test run failed: Instrumentation run failed due to 'java.lang.RuntimeException'

The testing then terminates early. Can anyone please advise what the correct code should be?

This is what I have:

package com.integral.troubleshooter.test;

import com.integral.troubleshooter.Question;
import com.integral.troubleshooter.R;
import com.integral.troubleshooter.TroubleShooterActivity;

import android.app.Activity;
import android.content.Intent;
import android.test.ActivityInstrumentationTestCase2;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class TroubleShooterActivityTest extends
    ActivityInstrumentationTestCase2<TroubleShooterActivity> {

private TroubleShooterActivity mActivity;
private Button mButton;
private TextView mTextView;
private ImageView mImageView;
private String resourceString;
private Activity nextActivity;

public TroubleShooterActivityTest() {      
    super("com.integral.troubleshooter.TroubleShooterActivity",        TroubleShooterActivity.class);    
}

 /*
 * Sets up the test environment before each test.
 * @see android.test.ActivityInstrumentationTestCase2#setUp()
 */
@Override
protected void setUp() throws Exception {

    super.setUp();

    setActivityInitialTouchMode(false);


    mActivity = getActivity();

    mButton = (Button)mActivity.findViewById(R.id.troubleShooter);

    mTextView =  (TextView)mActivity.findViewById(R.id.title);

    mImageView = (ImageView)mActivity.findViewById(R.id.IntegralLogo);

    resourceString = mActivity.getString(R.string.CustomerSupport); 
}

public void testPreconditions() {
    assertTrue(mTextView != null);
    assertTrue(mImageView != null);
}

public void testText(){
    assertEquals(resourceString,(String)mTextView.getText());
}

public void testButton(){
     mActivity.runOnUiThread(
                new Runnable() {
                    public void run() {
                       mButton.performClick();
                       nextActivity =  getActivity();
                       assertEquals(nextActivity, Question.class);
                    }
                }
            );



}
}

解决方案

Try to check if instrumentation is not present. Running this command will give a list of present instrumentation in your project environment:

$ adb shell pm list instrumentation

I hope this help.

这篇关于如何使用单元测试来测试一个按钮,启动了正确的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆