我正在尝试编写一个单元测试,用于测试按钮在单击时是否启动了正确的活动,但是我无法确定要使用的正确代码。 testButton方法确实运行并通过了测试,然后它会导致错误:
[2012-02-29 16:18:08 - TroubleShootingAppTest] Test run failed: Instrumentation run failed due to 'java.lang.RuntimeException'
测试然后提前终止。任何人都可以建议正确的代码应该是什么?
这就是我所拥有的:
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);
}
}
);
}
}
答案 0 :(得分:0)
尝试检查仪器是否不存在。运行此命令将在项目环境中提供当前检测的列表:
$ adb shell pm list instrumentation
我希望这有帮助。