我有一个简单的 HelloWorld Activity ,我尝试使用 Android JUnit 测试对其进行测试。应用程序本身运行正常,但测试失败并显示
“java.lang.RuntimeException:无法解析 Activity :Intent { action=android.intent.action.MAIN flags=0x10000000 comp={no.helloworld.HelloWorld/no.helloworld.HelloWorld} } 在 no.helloworld.test.HelloWorldTestcase.setUp(HelloWorldTestcase.java:21)"
这是我的 Activity 课:
package no.helloworld;
import android.app.Activity; import
android.os.Bundle;
public class HelloWorld extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
和测试:
public class HelloWorldTestcase extends 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();
}
为什么测试失败?该 Activity (当然)在 AndroidManifest.xml 中定义,应用程序按预期运行。
最佳答案
构造函数调用中的包应该匹配 list 中的检测目标。应该是“no.helloworld”而不是“no.helloworld.HelloWorld”
关于android - Android JUnit 测试中的运行时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3361896/