Android JUnit测试中的运行时异常

Sar*_*ara 3 junit android exception android-intent android-activity

我有一个简单的HelloWorld Activity,我尝试使用Android JUnit测试进行测试.应用程序本身就像应该运行一样,但测试失败了

"java.lang.RuntimeException:无法在no.helloworld.test上解析:Intent {action = android.intent.action.MAIN flags = 0x10000000 comp = {no.helloworld.HelloWorld/no.helloworld.HelloWorld}}的活动. HelloWorldTestcase.setUp(HelloWorldTestcase.java:21)"

这是我的活动课程:

包装no.helloworld;

import android.app.Activity; 进口

android.os.Bundle;

公共类HelloWorld扩展Activity {

@覆盖

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
} }
Run Code Online (Sandbox Code Playgroud)

而且测试:

公共类HelloWorldTestcase扩展ActivityInstrumentationTestCase2 {

private HelloWorld myActivity;
private TextView mView;
private String resourceString;
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)

为什么测试失败?该活动(当然)在AndroidManifest.xml中定义,应用程序按预期运行.

Fre*_*lin 7

构造函数调用中的包应与清单中的检测目标匹配.它应该是"no.helloworld"而不是"no.helloworld.HelloWorld"