Runtime Exception in Android JUnit testing

2019-05-10 21:03发布

I have a simple HelloWorld Activity that I try to test with an Android JUnit test. The application itself runs like it should but the test fails with an

"java.lang.RuntimeException: Unable to resolve activity for: Intent { action=android.intent.action.MAIN flags=0x10000000 comp={no.helloworld.HelloWorld/no.helloworld.HelloWorld} } at no.helloworld.test.HelloWorldTestcase.setUp(HelloWorldTestcase.java:21)"

This is my activity class:

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);
} }

And the test:

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();
}

Why does the test fail? The activity is (of course) defined in AndroidManifest.xml and the application runs as it should.

1条回答
手持菜刀,她持情操
2楼-- · 2019-05-10 21:46

The package in the constructor call should match the instrumentation target in the manifest. It should be "no.helloworld" instead of "no.helloworld.HelloWorld"

查看更多
登录 后发表回答