我有一个简单的HelloWorld Activity,我尝试使用Android JUnit测试进行测试。应用程序本身运行得如此,但测试失败并带有
“java.lang.RuntimeException:无法解析以下活动:Intent {action = android.intent.action.MAIN flags = 0x10000000 comp = {no.helloworld.HelloWorld / no.helloworld.HelloWorld}} at no.helloworld.test.HelloWorldTestcase.setUp(HelloWorldTestcase.java:21)“
这是我的活动课程:
包no.helloworld;
import android.app.Activity;进口
android.os.Bundle;
公共类HelloWorld扩展 活动{
@覆盖
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
测试:
公共类HelloWorldTestcase扩展了ActivityInstrumentationTestCase2 {
private HelloWorld myActivity; private TextView mView; private String resourceString;
public HelloWorldTestcase() {
super("no.helloworld.HelloWorld", HelloWorld.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
myActivity = this.getActivity();
mView = (TextView) myActivity.findViewById(no.helloworld.R.id.txt1);
resourceString = myActivity
.getString(no.helloworld.R.string.helloworld);
}
public void testPreconditions() {
assertNotNull(mView);
}
public void testText() {
assertEquals(resourceString, (String) mView.getText());
}
protected void tearDown() throws Exception {
super.tearDown();
}
为什么测试失败?该活动(当然)在AndroidManifest.xml中定义,应用程序按原样运行。
答案 0 :(得分:7)
构造函数调用中的包应与清单中的检测目标匹配。它应该是“no.helloworld”而不是“no.helloworld.HelloWorld”