Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runtime Exception in Android JUnit testing

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.

like image 549
Sara Avatar asked Sep 10 '26 16:09

Sara


1 Answers

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

like image 91
Fred Medlin Avatar answered Sep 13 '26 13:09

Fred Medlin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!