Android中的JUnit测试运行时异常 [英] Runtime Exception in Android JUnit testing

查看:171
本文介绍了Android中的JUnit测试运行时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的HelloWorld的活动,我尝试测试有一个Android JUnit测试。该应用程序本身运行像它应该,但测试失败与

了java.lang.RuntimeException:无法解析的活动:意向{行动= android.intent.action.MAIN标志= 0x10000000处补偿= {no.helloworld.HelloWorld / no.helloworld.HelloWorld}} 在no.helloworld.test.HelloWorldTestcase.setUp(HelloWorldTestcase.java:21)

这是我的活动类:

  

包no.helloworld;

     

进口android.app.Activity;进口

     

android.os.Bundle;

     

公共类的HelloWorld扩展   活动{

     

@覆盖

 公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
}}
 

和测试:

  

公共类HelloWorldTestcase扩展ActivityInstrumentationTestCase2 {

 私人的HelloWorld myActivity;
私人TextView的MView的;
私人字符串资源字符串;
 

 公共HelloWorldTestcase(){
    超(no.helloworld.HelloWorld,HelloWorld.class文件);
}

@覆盖
保护无效设置()抛出异常{
    super.setUp();
    myActivity = this.getActivity();
    MVIEW =(TextView中)myActivity.findViewById(no.helloworld.R.id.txt1);
    资源字符串= myActivity
            .getString(no.helloworld.R.string.helloworld);
}

公共无效测试preconditions(){
    assertNotNull(MVIEW);
}

公共无效testText(){
    的assertEquals(资源字符串(字符串)mView.getText());
}

保护无效tearDown的()抛出异常{
    super.tearDown();
}
 

为什么测试失败?该活动是(当然)在AndroidManifest.xml中定义的应用程序运行,因为它应该。

解决方案

在构造函数调用应该与仪器的目标清单中的包。它应该是no.helloworld,而不是no.helloworld.HelloWorld

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.

解决方案

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

这篇关于Android中的JUnit测试运行时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆