android - Android JUnit 测试中的运行时异常

标签 android exception junit android-activity android-intent

我有一个简单的 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/

相关文章:

java - 防止 hibernate 中的 NonUniqueObjectException

java - 如何避免超过工作表上允许的最大行数 jxl 中的异常

c# - 需要返回值的编译器 - 未注意到被调用方法中的无条件异常

java - 动画在 ViewPager 中滑动,图像发生变化

java - 使用 httpPost 从 java 发送时,node.js req.body 为空

java - Android Studio 2.1 : error: package org. junit 不存在

java - 在运行时动态添加 spring 上下文配置?

java - 测试时 JUnit 失败

android - GLSurfaceView.Renderer - 无法解析为类型

android - 如何确定Android中的Wifi,3G,4G(LTE)和VPN是哪个网络接口(interface)?